blob: d1f7e4ca5a5a38de3bb4729a1db89402e3a64d1b [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)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +053065#define CFG_ENABLE_MSG_FILTER_MASK (CFG_ENABLE_PM_MSG_FWD | \
66 CFG_ENABLE_INT_MSG_FWD | \
Bharat Kumar Gogada26b54be2017-01-31 14:29:30 +053067 CFG_ENABLE_ERR_MSG_FWD)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +053068
69/* Misc interrupt status mask bits */
70#define MSGF_MISC_SR_RXMSG_AVAIL BIT(0)
71#define MSGF_MISC_SR_RXMSG_OVER BIT(1)
72#define MSGF_MISC_SR_SLAVE_ERR BIT(4)
73#define MSGF_MISC_SR_MASTER_ERR BIT(5)
74#define MSGF_MISC_SR_I_ADDR_ERR BIT(6)
75#define MSGF_MISC_SR_E_ADDR_ERR BIT(7)
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +053076#define MSGF_MISC_SR_FATAL_AER BIT(16)
77#define MSGF_MISC_SR_NON_FATAL_AER BIT(17)
78#define MSGF_MISC_SR_CORR_AER BIT(18)
79#define MSGF_MISC_SR_UR_DETECT BIT(20)
80#define MSGF_MISC_SR_NON_FATAL_DEV BIT(22)
81#define MSGF_MISC_SR_FATAL_DEV BIT(23)
82#define MSGF_MISC_SR_LINK_DOWN BIT(24)
83#define MSGF_MSIC_SR_LINK_AUTO_BWIDTH BIT(25)
84#define MSGF_MSIC_SR_LINK_BWIDTH BIT(26)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +053085
86#define MSGF_MISC_SR_MASKALL (MSGF_MISC_SR_RXMSG_AVAIL | \
87 MSGF_MISC_SR_RXMSG_OVER | \
88 MSGF_MISC_SR_SLAVE_ERR | \
89 MSGF_MISC_SR_MASTER_ERR | \
90 MSGF_MISC_SR_I_ADDR_ERR | \
91 MSGF_MISC_SR_E_ADDR_ERR | \
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +053092 MSGF_MISC_SR_FATAL_AER | \
93 MSGF_MISC_SR_NON_FATAL_AER | \
94 MSGF_MISC_SR_CORR_AER | \
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +053095 MSGF_MISC_SR_UR_DETECT | \
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +053096 MSGF_MISC_SR_NON_FATAL_DEV | \
97 MSGF_MISC_SR_FATAL_DEV | \
98 MSGF_MISC_SR_LINK_DOWN | \
99 MSGF_MSIC_SR_LINK_AUTO_BWIDTH | \
100 MSGF_MSIC_SR_LINK_BWIDTH)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530101
102/* Legacy interrupt status mask bits */
103#define MSGF_LEG_SR_INTA BIT(0)
104#define MSGF_LEG_SR_INTB BIT(1)
105#define MSGF_LEG_SR_INTC BIT(2)
106#define MSGF_LEG_SR_INTD BIT(3)
107#define MSGF_LEG_SR_MASKALL (MSGF_LEG_SR_INTA | MSGF_LEG_SR_INTB | \
108 MSGF_LEG_SR_INTC | MSGF_LEG_SR_INTD)
109
110/* MSI interrupt status mask bits */
Bharat Kumar Gogadaf665bd12016-08-30 16:09:17 +0530111#define MSGF_MSI_SR_LO_MASK GENMASK(31, 0)
112#define MSGF_MSI_SR_HI_MASK GENMASK(31, 0)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530113
114#define MSII_PRESENT BIT(0)
115#define MSII_ENABLE BIT(0)
116#define MSII_STATUS_ENABLE BIT(15)
117
118/* Bridge config interrupt mask */
119#define BRCFG_INTERRUPT_MASK BIT(0)
120#define BREG_PRESENT BIT(0)
121#define BREG_ENABLE BIT(0)
122#define BREG_ENABLE_FORCE BIT(1)
123
124/* E_ECAM status mask bits */
125#define E_ECAM_PRESENT BIT(0)
126#define E_ECAM_CR_ENABLE BIT(0)
127#define E_ECAM_SIZE_LOC GENMASK(20, 16)
128#define E_ECAM_SIZE_SHIFT 16
129#define ECAM_BUS_LOC_SHIFT 20
130#define ECAM_DEV_LOC_SHIFT 12
131#define NWL_ECAM_VALUE_DEFAULT 12
132
133#define CFG_DMA_REG_BAR GENMASK(2, 0)
134
135#define INT_PCI_MSI_NR (2 * 32)
136#define INTX_NUM 4
137
138/* Readin the PS_LINKUP */
139#define PS_LINKUP_OFFSET 0x00000238
140#define PCIE_PHY_LINKUP_BIT BIT(0)
141#define PHY_RDY_LINKUP_BIT BIT(1)
142
143/* Parameters for the waiting for link up routine */
144#define LINK_WAIT_MAX_RETRIES 10
145#define LINK_WAIT_USLEEP_MIN 90000
146#define LINK_WAIT_USLEEP_MAX 100000
147
148struct nwl_msi { /* MSI information */
149 struct irq_domain *msi_domain;
150 unsigned long *bitmap;
151 struct irq_domain *dev_domain;
152 struct mutex lock; /* protect bitmap variable */
153 int irq_msi0;
154 int irq_msi1;
155};
156
157struct nwl_pcie {
158 struct device *dev;
159 void __iomem *breg_base;
160 void __iomem *pcireg_base;
161 void __iomem *ecam_base;
162 phys_addr_t phys_breg_base; /* Physical Bridge Register Base */
163 phys_addr_t phys_pcie_reg_base; /* Physical PCIe Controller Base */
164 phys_addr_t phys_ecam_base; /* Physical Configuration Base */
165 u32 breg_size;
166 u32 pcie_reg_size;
167 u32 ecam_size;
168 int irq_intx;
169 int irq_misc;
170 u32 ecam_value;
171 u8 last_busno;
172 u8 root_busno;
173 struct nwl_msi msi;
174 struct irq_domain *legacy_irq_domain;
175};
176
177static inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
178{
179 return readl(pcie->breg_base + off);
180}
181
182static inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
183{
184 writel(val, pcie->breg_base + off);
185}
186
187static bool nwl_pcie_link_up(struct nwl_pcie *pcie)
188{
189 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
190 return true;
191 return false;
192}
193
194static bool nwl_phy_link_up(struct nwl_pcie *pcie)
195{
196 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
197 return true;
198 return false;
199}
200
201static int nwl_wait_for_link(struct nwl_pcie *pcie)
202{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500203 struct device *dev = pcie->dev;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530204 int retries;
205
206 /* check if the link is up or not */
207 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
208 if (nwl_phy_link_up(pcie))
209 return 0;
210 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
211 }
212
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500213 dev_err(dev, "PHY link never came up\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530214 return -ETIMEDOUT;
215}
216
217static bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
218{
219 struct nwl_pcie *pcie = bus->sysdata;
220
221 /* Check link before accessing downstream ports */
222 if (bus->number != pcie->root_busno) {
223 if (!nwl_pcie_link_up(pcie))
224 return false;
225 }
226
227 /* Only one device down on each root port */
228 if (bus->number == pcie->root_busno && devfn > 0)
229 return false;
230
231 return true;
232}
233
234/**
235 * nwl_pcie_map_bus - Get configuration base
236 *
237 * @bus: Bus structure of current bus
238 * @devfn: Device/function
239 * @where: Offset from base
240 *
241 * Return: Base address of the configuration space needed to be
242 * accessed.
243 */
244static void __iomem *nwl_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
245 int where)
246{
247 struct nwl_pcie *pcie = bus->sysdata;
248 int relbus;
249
250 if (!nwl_pcie_valid_device(bus, devfn))
251 return NULL;
252
253 relbus = (bus->number << ECAM_BUS_LOC_SHIFT) |
254 (devfn << ECAM_DEV_LOC_SHIFT);
255
256 return pcie->ecam_base + relbus + where;
257}
258
259/* PCIe operations */
260static struct pci_ops nwl_pcie_ops = {
261 .map_bus = nwl_pcie_map_bus,
262 .read = pci_generic_config_read,
263 .write = pci_generic_config_write,
264};
265
266static irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
267{
268 struct nwl_pcie *pcie = data;
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500269 struct device *dev = pcie->dev;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530270 u32 misc_stat;
271
272 /* Checking for misc interrupts */
273 misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
274 MSGF_MISC_SR_MASKALL;
275 if (!misc_stat)
276 return IRQ_NONE;
277
278 if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500279 dev_err(dev, "Received Message FIFO Overflow\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530280
281 if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500282 dev_err(dev, "Slave error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530283
284 if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500285 dev_err(dev, "Master error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530286
287 if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500288 dev_err(dev, "In Misc Ingress address translation error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530289
290 if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500291 dev_err(dev, "In Misc Egress address translation error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530292
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530293 if (misc_stat & MSGF_MISC_SR_FATAL_AER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500294 dev_err(dev, "Fatal Error in AER Capability\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530295
296 if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500297 dev_err(dev, "Non-Fatal Error in AER Capability\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530298
299 if (misc_stat & MSGF_MISC_SR_CORR_AER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500300 dev_err(dev, "Correctable Error in AER Capability\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530301
302 if (misc_stat & MSGF_MISC_SR_UR_DETECT)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500303 dev_err(dev, "Unsupported request Detected\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530304
305 if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500306 dev_err(dev, "Non-Fatal Error Detected\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530307
308 if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500309 dev_err(dev, "Fatal Error Detected\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530310
311 if (misc_stat & MSGF_MSIC_SR_LINK_AUTO_BWIDTH)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500312 dev_info(dev, "Link Autonomous Bandwidth Management Status bit set\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530313
314 if (misc_stat & MSGF_MSIC_SR_LINK_BWIDTH)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500315 dev_info(dev, "Link Bandwidth Management Status bit set\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530316
317 /* Clear misc interrupt status */
318 nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
319
320 return IRQ_HANDLED;
321}
322
323static void nwl_pcie_leg_handler(struct irq_desc *desc)
324{
325 struct irq_chip *chip = irq_desc_get_chip(desc);
326 struct nwl_pcie *pcie;
327 unsigned long status;
328 u32 bit;
329 u32 virq;
330
331 chained_irq_enter(chip, desc);
332 pcie = irq_desc_get_handler_data(desc);
333
334 while ((status = nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
335 MSGF_LEG_SR_MASKALL) != 0) {
336 for_each_set_bit(bit, &status, INTX_NUM) {
337 virq = irq_find_mapping(pcie->legacy_irq_domain,
338 bit + 1);
339 if (virq)
340 generic_handle_irq(virq);
341 }
342 }
343
344 chained_irq_exit(chip, desc);
345}
346
347static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
348{
349 struct nwl_msi *msi;
350 unsigned long status;
351 u32 bit;
352 u32 virq;
353
354 msi = &pcie->msi;
355
356 while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
357 for_each_set_bit(bit, &status, 32) {
358 nwl_bridge_writel(pcie, 1 << bit, status_reg);
359 virq = irq_find_mapping(msi->dev_domain, bit);
360 if (virq)
361 generic_handle_irq(virq);
362 }
363 }
364}
365
366static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
367{
368 struct irq_chip *chip = irq_desc_get_chip(desc);
369 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
370
371 chained_irq_enter(chip, desc);
372 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
373 chained_irq_exit(chip, desc);
374}
375
376static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
377{
378 struct irq_chip *chip = irq_desc_get_chip(desc);
379 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
380
381 chained_irq_enter(chip, desc);
382 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
383 chained_irq_exit(chip, desc);
384}
385
386static int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
387 irq_hw_number_t hwirq)
388{
389 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
390 irq_set_chip_data(irq, domain->host_data);
391
392 return 0;
393}
394
395static const struct irq_domain_ops legacy_domain_ops = {
396 .map = nwl_legacy_map,
397};
398
399#ifdef CONFIG_PCI_MSI
400static struct irq_chip nwl_msi_irq_chip = {
401 .name = "nwl_pcie:msi",
402 .irq_enable = unmask_msi_irq,
403 .irq_disable = mask_msi_irq,
404 .irq_mask = mask_msi_irq,
405 .irq_unmask = unmask_msi_irq,
406
407};
408
409static struct msi_domain_info nwl_msi_domain_info = {
410 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
411 MSI_FLAG_MULTI_PCI_MSI),
412 .chip = &nwl_msi_irq_chip,
413};
414#endif
415
416static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
417{
418 struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
419 phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
420
421 msg->address_lo = lower_32_bits(msi_addr);
422 msg->address_hi = upper_32_bits(msi_addr);
423 msg->data = data->hwirq;
424}
425
426static int nwl_msi_set_affinity(struct irq_data *irq_data,
427 const struct cpumask *mask, bool force)
428{
429 return -EINVAL;
430}
431
432static struct irq_chip nwl_irq_chip = {
433 .name = "Xilinx MSI",
434 .irq_compose_msi_msg = nwl_compose_msi_msg,
435 .irq_set_affinity = nwl_msi_set_affinity,
436};
437
438static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
439 unsigned int nr_irqs, void *args)
440{
441 struct nwl_pcie *pcie = domain->host_data;
442 struct nwl_msi *msi = &pcie->msi;
443 int bit;
444 int i;
445
446 mutex_lock(&msi->lock);
447 bit = bitmap_find_next_zero_area(msi->bitmap, INT_PCI_MSI_NR, 0,
448 nr_irqs, 0);
449 if (bit >= INT_PCI_MSI_NR) {
450 mutex_unlock(&msi->lock);
451 return -ENOSPC;
452 }
453
454 bitmap_set(msi->bitmap, bit, nr_irqs);
455
456 for (i = 0; i < nr_irqs; i++) {
457 irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
458 domain->host_data, handle_simple_irq,
459 NULL, NULL);
460 }
461 mutex_unlock(&msi->lock);
462 return 0;
463}
464
465static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
466 unsigned int nr_irqs)
467{
468 struct irq_data *data = irq_domain_get_irq_data(domain, virq);
469 struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
470 struct nwl_msi *msi = &pcie->msi;
471
472 mutex_lock(&msi->lock);
473 bitmap_clear(msi->bitmap, data->hwirq, nr_irqs);
474 mutex_unlock(&msi->lock);
475}
476
477static const struct irq_domain_ops dev_msi_domain_ops = {
478 .alloc = nwl_irq_domain_alloc,
479 .free = nwl_irq_domain_free,
480};
481
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530482static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
483{
484#ifdef CONFIG_PCI_MSI
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500485 struct device *dev = pcie->dev;
486 struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530487 struct nwl_msi *msi = &pcie->msi;
488
489 msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
490 &dev_msi_domain_ops, pcie);
491 if (!msi->dev_domain) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500492 dev_err(dev, "failed to create dev IRQ domain\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530493 return -ENOMEM;
494 }
495 msi->msi_domain = pci_msi_create_irq_domain(fwnode,
496 &nwl_msi_domain_info,
497 msi->dev_domain);
498 if (!msi->msi_domain) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500499 dev_err(dev, "failed to create msi IRQ domain\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530500 irq_domain_remove(msi->dev_domain);
501 return -ENOMEM;
502 }
503#endif
504 return 0;
505}
506
507static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
508{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500509 struct device *dev = pcie->dev;
510 struct device_node *node = dev->of_node;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530511 struct device_node *legacy_intc_node;
512
513 legacy_intc_node = of_get_next_child(node, NULL);
514 if (!legacy_intc_node) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500515 dev_err(dev, "No legacy intc node found\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530516 return -EINVAL;
517 }
518
519 pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
520 INTX_NUM,
521 &legacy_domain_ops,
522 pcie);
523
524 if (!pcie->legacy_irq_domain) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500525 dev_err(dev, "failed to create IRQ domain\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530526 return -ENOMEM;
527 }
528
529 nwl_pcie_init_msi_irq_domain(pcie);
530 return 0;
531}
532
Lorenzo Pieralisi5cbd6782017-06-28 15:13:50 -0500533static int nwl_pcie_enable_msi(struct nwl_pcie *pcie)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530534{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500535 struct device *dev = pcie->dev;
536 struct platform_device *pdev = to_platform_device(dev);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530537 struct nwl_msi *msi = &pcie->msi;
538 unsigned long base;
539 int ret;
540 int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
541
542 mutex_init(&msi->lock);
543
544 msi->bitmap = kzalloc(size, GFP_KERNEL);
545 if (!msi->bitmap)
546 return -ENOMEM;
547
548 /* Get msi_1 IRQ number */
549 msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
550 if (msi->irq_msi1 < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500551 dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi1);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530552 ret = -EINVAL;
553 goto err;
554 }
555
556 irq_set_chained_handler_and_data(msi->irq_msi1,
557 nwl_pcie_msi_handler_high, pcie);
558
559 /* Get msi_0 IRQ number */
560 msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
561 if (msi->irq_msi0 < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500562 dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi0);
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_msi0,
568 nwl_pcie_msi_handler_low, pcie);
569
570 /* Check for msii_present bit */
571 ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
572 if (!ret) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500573 dev_err(dev, "MSI not present\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530574 ret = -EIO;
575 goto err;
576 }
577
578 /* Enable MSII */
579 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
580 MSII_ENABLE, I_MSII_CONTROL);
581
582 /* Enable MSII status */
583 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
584 MSII_STATUS_ENABLE, I_MSII_CONTROL);
585
586 /* setup AFI/FPCI range */
587 base = pcie->phys_pcie_reg_base;
588 nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
589 nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
590
591 /*
592 * For high range MSI interrupts: disable, clear any pending,
593 * and enable
594 */
595 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
596
597 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
598 MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
599
600 nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
601
602 /*
603 * For low range MSI interrupts: disable, clear any pending,
604 * and enable
605 */
606 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
607
608 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
609 MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
610
611 nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
612
613 return 0;
614err:
615 kfree(msi->bitmap);
616 msi->bitmap = NULL;
617 return ret;
618}
619
620static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
621{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500622 struct device *dev = pcie->dev;
623 struct platform_device *pdev = to_platform_device(dev);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530624 u32 breg_val, ecam_val, first_busno = 0;
625 int err;
626
627 breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
628 if (!breg_val) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500629 dev_err(dev, "BREG is not present\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530630 return breg_val;
631 }
632
633 /* Write bridge_off to breg base */
634 nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
635 E_BREG_BASE_LO);
636 nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
637 E_BREG_BASE_HI);
638
639 /* Enable BREG */
640 nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
641 E_BREG_CONTROL);
642
643 /* Disable DMA channel registers */
644 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
645 CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
646
647 /* Enable Ingress subtractive decode translation */
648 nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
649
650 /* Enable msg filtering details */
651 nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
652 BRCFG_PCIE_RX_MSG_FILTER);
653
654 err = nwl_wait_for_link(pcie);
655 if (err)
656 return err;
657
658 ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
659 if (!ecam_val) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500660 dev_err(dev, "ECAM is not present\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530661 return ecam_val;
662 }
663
664 /* Enable ECAM */
665 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
666 E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
667
668 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
669 (pcie->ecam_value << E_ECAM_SIZE_SHIFT),
670 E_ECAM_CONTROL);
671
672 nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
673 E_ECAM_BASE_LO);
674 nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
675 E_ECAM_BASE_HI);
676
677 /* Get bus range */
678 ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
679 pcie->last_busno = (ecam_val & E_ECAM_SIZE_LOC) >> E_ECAM_SIZE_SHIFT;
680 /* Write primary, secondary and subordinate bus numbers */
681 ecam_val = first_busno;
682 ecam_val |= (first_busno + 1) << 8;
683 ecam_val |= (pcie->last_busno << E_ECAM_SIZE_SHIFT);
684 writel(ecam_val, (pcie->ecam_base + PCI_PRIMARY_BUS));
685
686 if (nwl_pcie_link_up(pcie))
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500687 dev_info(dev, "Link is UP\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530688 else
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500689 dev_info(dev, "Link is DOWN\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530690
691 /* Get misc IRQ number */
692 pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
693 if (pcie->irq_misc < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500694 dev_err(dev, "failed to get misc IRQ %d\n",
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530695 pcie->irq_misc);
696 return -EINVAL;
697 }
698
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500699 err = devm_request_irq(dev, pcie->irq_misc,
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530700 nwl_pcie_misc_handler, IRQF_SHARED,
701 "nwl_pcie:misc", pcie);
702 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500703 dev_err(dev, "fail to register misc IRQ#%d\n",
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530704 pcie->irq_misc);
705 return err;
706 }
707
708 /* Disable all misc interrupts */
709 nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
710
711 /* Clear pending misc interrupts */
712 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
713 MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
714
715 /* Enable all misc interrupts */
716 nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
717
718
719 /* Disable all legacy interrupts */
720 nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
721
722 /* Clear pending legacy interrupts */
723 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
724 MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
725
726 /* Enable all legacy interrupts */
727 nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
728
729 /* Enable the bridge config interrupt */
730 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
731 BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
732
733 return 0;
734}
735
736static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
737 struct platform_device *pdev)
738{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500739 struct device *dev = pcie->dev;
740 struct device_node *node = dev->of_node;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530741 struct resource *res;
742 const char *type;
743
744 /* Check for device type */
745 type = of_get_property(node, "device_type", NULL);
746 if (!type || strcmp(type, "pci")) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500747 dev_err(dev, "invalid \"device_type\" %s\n", type);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530748 return -EINVAL;
749 }
750
751 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500752 pcie->breg_base = devm_ioremap_resource(dev, res);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530753 if (IS_ERR(pcie->breg_base))
754 return PTR_ERR(pcie->breg_base);
755 pcie->phys_breg_base = res->start;
756
757 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500758 pcie->pcireg_base = devm_ioremap_resource(dev, res);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530759 if (IS_ERR(pcie->pcireg_base))
760 return PTR_ERR(pcie->pcireg_base);
761 pcie->phys_pcie_reg_base = res->start;
762
763 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
Lorenzo Pieralisicd00f082017-04-19 17:48:58 +0100764 pcie->ecam_base = devm_pci_remap_cfg_resource(dev, res);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530765 if (IS_ERR(pcie->ecam_base))
766 return PTR_ERR(pcie->ecam_base);
767 pcie->phys_ecam_base = res->start;
768
769 /* Get intx IRQ number */
770 pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
771 if (pcie->irq_intx < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500772 dev_err(dev, "failed to get intx IRQ %d\n", pcie->irq_intx);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530773 return -EINVAL;
774 }
775
776 irq_set_chained_handler_and_data(pcie->irq_intx,
777 nwl_pcie_leg_handler, pcie);
778
779 return 0;
780}
781
782static const struct of_device_id nwl_pcie_of_match[] = {
783 { .compatible = "xlnx,nwl-pcie-2.11", },
784 {}
785};
786
787static int nwl_pcie_probe(struct platform_device *pdev)
788{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500789 struct device *dev = &pdev->dev;
790 struct device_node *node = dev->of_node;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530791 struct nwl_pcie *pcie;
792 struct pci_bus *bus;
793 struct pci_bus *child;
Lorenzo Pieralisi123db532017-06-28 15:14:01 -0500794 struct pci_host_bridge *bridge;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530795 int err;
796 resource_size_t iobase = 0;
797 LIST_HEAD(res);
798
Lorenzo Pieralisi123db532017-06-28 15:14:01 -0500799 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
800 if (!bridge)
801 return -ENODEV;
802
803 pcie = pci_host_bridge_priv(bridge);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530804
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500805 pcie->dev = dev;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530806 pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
807
808 err = nwl_pcie_parse_dt(pcie, pdev);
809 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500810 dev_err(dev, "Parsing DT failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530811 return err;
812 }
813
814 err = nwl_pcie_bridge_init(pcie);
815 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500816 dev_err(dev, "HW Initialization failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530817 return err;
818 }
819
820 err = of_pci_get_host_bridge_resources(node, 0, 0xff, &res, &iobase);
821 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500822 dev_err(dev, "Getting bridge resources failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530823 return err;
824 }
825
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500826 err = devm_request_pci_bus_resources(dev, &res);
Bjorn Helgaas21f7fc242016-05-28 18:24:36 -0500827 if (err)
828 goto error;
829
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530830 err = nwl_pcie_init_irq_domain(pcie);
831 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500832 dev_err(dev, "Failed creating IRQ Domain\n");
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500833 goto error;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530834 }
835
Lorenzo Pieralisi123db532017-06-28 15:14:01 -0500836 list_splice_init(&res, &bridge->windows);
837 bridge->dev.parent = dev;
838 bridge->sysdata = pcie;
839 bridge->busnr = pcie->root_busno;
840 bridge->ops = &nwl_pcie_ops;
Lorenzo Pieralisi1ee4d932017-06-28 15:14:11 -0500841 bridge->map_irq = of_irq_parse_and_map_pci;
842 bridge->swizzle_irq = pci_common_swizzle;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530843
844 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Lorenzo Pieralisi5cbd6782017-06-28 15:13:50 -0500845 err = nwl_pcie_enable_msi(pcie);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530846 if (err < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500847 dev_err(dev, "failed to enable MSI support: %d\n", err);
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500848 goto error;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530849 }
850 }
Lorenzo Pieralisi123db532017-06-28 15:14:01 -0500851
852 err = pci_scan_root_bus_bridge(bridge);
853 if (err)
854 goto error;
855
856 bus = bridge->bus;
857
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530858 pci_assign_unassigned_bus_resources(bus);
859 list_for_each_entry(child, &bus->children, node)
860 pcie_bus_configure_settings(child);
861 pci_bus_add_devices(bus);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530862 return 0;
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500863
864error:
865 pci_free_resource_list(&res);
866 return err;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530867}
868
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530869static struct platform_driver nwl_pcie_driver = {
870 .driver = {
871 .name = "nwl-pcie",
Paul Gortmakerff187e772016-08-24 16:57:50 -0400872 .suppress_bind_attrs = true,
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530873 .of_match_table = nwl_pcie_of_match,
874 },
875 .probe = nwl_pcie_probe,
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530876};
Paul Gortmakerff187e772016-08-24 16:57:50 -0400877builtin_platform_driver(nwl_pcie_driver);