10c87f90bSSrikanth Thokala // SPDX-License-Identifier: GPL-2.0-only
20c87f90bSSrikanth Thokala /*
30c87f90bSSrikanth Thokala * PCIe controller driver for Intel Keem Bay
40c87f90bSSrikanth Thokala * Copyright (C) 2020 Intel Corporation
50c87f90bSSrikanth Thokala */
60c87f90bSSrikanth Thokala
70c87f90bSSrikanth Thokala #include <linux/bitfield.h>
80c87f90bSSrikanth Thokala #include <linux/bits.h>
90c87f90bSSrikanth Thokala #include <linux/clk.h>
100c87f90bSSrikanth Thokala #include <linux/delay.h>
110c87f90bSSrikanth Thokala #include <linux/err.h>
120c87f90bSSrikanth Thokala #include <linux/gpio/consumer.h>
130c87f90bSSrikanth Thokala #include <linux/init.h>
140c87f90bSSrikanth Thokala #include <linux/iopoll.h>
150c87f90bSSrikanth Thokala #include <linux/irqchip/chained_irq.h>
160c87f90bSSrikanth Thokala #include <linux/kernel.h>
170c87f90bSSrikanth Thokala #include <linux/mod_devicetable.h>
180c87f90bSSrikanth Thokala #include <linux/pci.h>
190c87f90bSSrikanth Thokala #include <linux/platform_device.h>
200c87f90bSSrikanth Thokala #include <linux/property.h>
210c87f90bSSrikanth Thokala
220c87f90bSSrikanth Thokala #include "pcie-designware.h"
230c87f90bSSrikanth Thokala
240c87f90bSSrikanth Thokala /* PCIE_REGS_APB_SLV Registers */
250c87f90bSSrikanth Thokala #define PCIE_REGS_PCIE_CFG 0x0004
260c87f90bSSrikanth Thokala #define PCIE_DEVICE_TYPE BIT(8)
270c87f90bSSrikanth Thokala #define PCIE_RSTN BIT(0)
280c87f90bSSrikanth Thokala #define PCIE_REGS_PCIE_APP_CNTRL 0x0008
290c87f90bSSrikanth Thokala #define APP_LTSSM_ENABLE BIT(0)
300c87f90bSSrikanth Thokala #define PCIE_REGS_INTERRUPT_ENABLE 0x0028
310c87f90bSSrikanth Thokala #define MSI_CTRL_INT_EN BIT(8)
320c87f90bSSrikanth Thokala #define EDMA_INT_EN GENMASK(7, 0)
330c87f90bSSrikanth Thokala #define PCIE_REGS_INTERRUPT_STATUS 0x002c
340c87f90bSSrikanth Thokala #define MSI_CTRL_INT BIT(8)
350c87f90bSSrikanth Thokala #define PCIE_REGS_PCIE_SII_PM_STATE 0x00b0
360c87f90bSSrikanth Thokala #define SMLH_LINK_UP BIT(19)
370c87f90bSSrikanth Thokala #define RDLH_LINK_UP BIT(8)
380c87f90bSSrikanth Thokala #define PCIE_REGS_PCIE_SII_LINK_UP (SMLH_LINK_UP | RDLH_LINK_UP)
390c87f90bSSrikanth Thokala #define PCIE_REGS_PCIE_PHY_CNTL 0x0164
400c87f90bSSrikanth Thokala #define PHY0_SRAM_BYPASS BIT(8)
410c87f90bSSrikanth Thokala #define PCIE_REGS_PCIE_PHY_STAT 0x0168
420c87f90bSSrikanth Thokala #define PHY0_MPLLA_STATE BIT(1)
430c87f90bSSrikanth Thokala #define PCIE_REGS_LJPLL_STA 0x016c
440c87f90bSSrikanth Thokala #define LJPLL_LOCK BIT(0)
450c87f90bSSrikanth Thokala #define PCIE_REGS_LJPLL_CNTRL_0 0x0170
460c87f90bSSrikanth Thokala #define LJPLL_EN BIT(29)
470c87f90bSSrikanth Thokala #define LJPLL_FOUT_EN GENMASK(24, 21)
480c87f90bSSrikanth Thokala #define PCIE_REGS_LJPLL_CNTRL_2 0x0178
490c87f90bSSrikanth Thokala #define LJPLL_REF_DIV GENMASK(17, 12)
500c87f90bSSrikanth Thokala #define LJPLL_FB_DIV GENMASK(11, 0)
510c87f90bSSrikanth Thokala #define PCIE_REGS_LJPLL_CNTRL_3 0x017c
520c87f90bSSrikanth Thokala #define LJPLL_POST_DIV3A GENMASK(24, 22)
530c87f90bSSrikanth Thokala #define LJPLL_POST_DIV2A GENMASK(18, 16)
540c87f90bSSrikanth Thokala
550c87f90bSSrikanth Thokala #define PERST_DELAY_US 1000
560c87f90bSSrikanth Thokala #define AUX_CLK_RATE_HZ 24000000
570c87f90bSSrikanth Thokala
580c87f90bSSrikanth Thokala struct keembay_pcie {
590c87f90bSSrikanth Thokala struct dw_pcie pci;
600c87f90bSSrikanth Thokala void __iomem *apb_base;
610c87f90bSSrikanth Thokala enum dw_pcie_device_mode mode;
620c87f90bSSrikanth Thokala
630c87f90bSSrikanth Thokala struct clk *clk_master;
640c87f90bSSrikanth Thokala struct clk *clk_aux;
650c87f90bSSrikanth Thokala struct gpio_desc *reset;
660c87f90bSSrikanth Thokala };
670c87f90bSSrikanth Thokala
680c87f90bSSrikanth Thokala struct keembay_pcie_of_data {
690c87f90bSSrikanth Thokala enum dw_pcie_device_mode mode;
700c87f90bSSrikanth Thokala };
710c87f90bSSrikanth Thokala
keembay_ep_reset_assert(struct keembay_pcie * pcie)720c87f90bSSrikanth Thokala static void keembay_ep_reset_assert(struct keembay_pcie *pcie)
730c87f90bSSrikanth Thokala {
740c87f90bSSrikanth Thokala gpiod_set_value_cansleep(pcie->reset, 1);
750c87f90bSSrikanth Thokala usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
760c87f90bSSrikanth Thokala }
770c87f90bSSrikanth Thokala
keembay_ep_reset_deassert(struct keembay_pcie * pcie)780c87f90bSSrikanth Thokala static void keembay_ep_reset_deassert(struct keembay_pcie *pcie)
790c87f90bSSrikanth Thokala {
800c87f90bSSrikanth Thokala /*
810c87f90bSSrikanth Thokala * Ensure that PERST# is asserted for a minimum of 100ms.
820c87f90bSSrikanth Thokala *
830c87f90bSSrikanth Thokala * For more details, refer to PCI Express Card Electromechanical
840c87f90bSSrikanth Thokala * Specification Revision 1.1, Table-2.4.
850c87f90bSSrikanth Thokala */
860c87f90bSSrikanth Thokala msleep(100);
870c87f90bSSrikanth Thokala
880c87f90bSSrikanth Thokala gpiod_set_value_cansleep(pcie->reset, 0);
890c87f90bSSrikanth Thokala usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
900c87f90bSSrikanth Thokala }
910c87f90bSSrikanth Thokala
keembay_pcie_ltssm_set(struct keembay_pcie * pcie,bool enable)920c87f90bSSrikanth Thokala static void keembay_pcie_ltssm_set(struct keembay_pcie *pcie, bool enable)
930c87f90bSSrikanth Thokala {
940c87f90bSSrikanth Thokala u32 val;
950c87f90bSSrikanth Thokala
960c87f90bSSrikanth Thokala val = readl(pcie->apb_base + PCIE_REGS_PCIE_APP_CNTRL);
970c87f90bSSrikanth Thokala if (enable)
980c87f90bSSrikanth Thokala val |= APP_LTSSM_ENABLE;
990c87f90bSSrikanth Thokala else
1000c87f90bSSrikanth Thokala val &= ~APP_LTSSM_ENABLE;
1010c87f90bSSrikanth Thokala writel(val, pcie->apb_base + PCIE_REGS_PCIE_APP_CNTRL);
1020c87f90bSSrikanth Thokala }
1030c87f90bSSrikanth Thokala
keembay_pcie_link_up(struct dw_pcie * pci)1040c87f90bSSrikanth Thokala static int keembay_pcie_link_up(struct dw_pcie *pci)
1050c87f90bSSrikanth Thokala {
1060c87f90bSSrikanth Thokala struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
1070c87f90bSSrikanth Thokala u32 val;
1080c87f90bSSrikanth Thokala
1090c87f90bSSrikanth Thokala val = readl(pcie->apb_base + PCIE_REGS_PCIE_SII_PM_STATE);
1100c87f90bSSrikanth Thokala
1110c87f90bSSrikanth Thokala return (val & PCIE_REGS_PCIE_SII_LINK_UP) == PCIE_REGS_PCIE_SII_LINK_UP;
1120c87f90bSSrikanth Thokala }
1130c87f90bSSrikanth Thokala
keembay_pcie_start_link(struct dw_pcie * pci)1140c87f90bSSrikanth Thokala static int keembay_pcie_start_link(struct dw_pcie *pci)
1150c87f90bSSrikanth Thokala {
1160c87f90bSSrikanth Thokala struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
1170c87f90bSSrikanth Thokala u32 val;
1180c87f90bSSrikanth Thokala int ret;
1190c87f90bSSrikanth Thokala
1200c87f90bSSrikanth Thokala if (pcie->mode == DW_PCIE_EP_TYPE)
1210c87f90bSSrikanth Thokala return 0;
1220c87f90bSSrikanth Thokala
1230c87f90bSSrikanth Thokala keembay_pcie_ltssm_set(pcie, false);
1240c87f90bSSrikanth Thokala
1250c87f90bSSrikanth Thokala ret = readl_poll_timeout(pcie->apb_base + PCIE_REGS_PCIE_PHY_STAT,
1260c87f90bSSrikanth Thokala val, val & PHY0_MPLLA_STATE, 20,
1270c87f90bSSrikanth Thokala 500 * USEC_PER_MSEC);
1280c87f90bSSrikanth Thokala if (ret) {
1290c87f90bSSrikanth Thokala dev_err(pci->dev, "MPLLA is not locked\n");
1300c87f90bSSrikanth Thokala return ret;
1310c87f90bSSrikanth Thokala }
1320c87f90bSSrikanth Thokala
1330c87f90bSSrikanth Thokala keembay_pcie_ltssm_set(pcie, true);
1340c87f90bSSrikanth Thokala
1350c87f90bSSrikanth Thokala return 0;
1360c87f90bSSrikanth Thokala }
1370c87f90bSSrikanth Thokala
keembay_pcie_stop_link(struct dw_pcie * pci)1380c87f90bSSrikanth Thokala static void keembay_pcie_stop_link(struct dw_pcie *pci)
1390c87f90bSSrikanth Thokala {
1400c87f90bSSrikanth Thokala struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
1410c87f90bSSrikanth Thokala
1420c87f90bSSrikanth Thokala keembay_pcie_ltssm_set(pcie, false);
1430c87f90bSSrikanth Thokala }
1440c87f90bSSrikanth Thokala
1450c87f90bSSrikanth Thokala static const struct dw_pcie_ops keembay_pcie_ops = {
1460c87f90bSSrikanth Thokala .link_up = keembay_pcie_link_up,
1470c87f90bSSrikanth Thokala .start_link = keembay_pcie_start_link,
1480c87f90bSSrikanth Thokala .stop_link = keembay_pcie_stop_link,
1490c87f90bSSrikanth Thokala };
1500c87f90bSSrikanth Thokala
keembay_pcie_disable_clock(void * data)151*7a653169SKrzysztof Wilczyński static inline void keembay_pcie_disable_clock(void *data)
152*7a653169SKrzysztof Wilczyński {
153*7a653169SKrzysztof Wilczyński struct clk *clk = data;
154*7a653169SKrzysztof Wilczyński
155*7a653169SKrzysztof Wilczyński clk_disable_unprepare(clk);
156*7a653169SKrzysztof Wilczyński }
157*7a653169SKrzysztof Wilczyński
keembay_pcie_probe_clock(struct device * dev,const char * id,u64 rate)1580c87f90bSSrikanth Thokala static inline struct clk *keembay_pcie_probe_clock(struct device *dev,
1590c87f90bSSrikanth Thokala const char *id, u64 rate)
1600c87f90bSSrikanth Thokala {
1610c87f90bSSrikanth Thokala struct clk *clk;
1620c87f90bSSrikanth Thokala int ret;
1630c87f90bSSrikanth Thokala
1640c87f90bSSrikanth Thokala clk = devm_clk_get(dev, id);
1650c87f90bSSrikanth Thokala if (IS_ERR(clk))
1660c87f90bSSrikanth Thokala return clk;
1670c87f90bSSrikanth Thokala
1680c87f90bSSrikanth Thokala if (rate) {
1690c87f90bSSrikanth Thokala ret = clk_set_rate(clk, rate);
1700c87f90bSSrikanth Thokala if (ret)
1710c87f90bSSrikanth Thokala return ERR_PTR(ret);
1720c87f90bSSrikanth Thokala }
1730c87f90bSSrikanth Thokala
1740c87f90bSSrikanth Thokala ret = clk_prepare_enable(clk);
1750c87f90bSSrikanth Thokala if (ret)
1760c87f90bSSrikanth Thokala return ERR_PTR(ret);
1770c87f90bSSrikanth Thokala
178*7a653169SKrzysztof Wilczyński ret = devm_add_action_or_reset(dev, keembay_pcie_disable_clock, clk);
1790c87f90bSSrikanth Thokala if (ret)
1800c87f90bSSrikanth Thokala return ERR_PTR(ret);
1810c87f90bSSrikanth Thokala
1820c87f90bSSrikanth Thokala return clk;
1830c87f90bSSrikanth Thokala }
1840c87f90bSSrikanth Thokala
keembay_pcie_probe_clocks(struct keembay_pcie * pcie)1850c87f90bSSrikanth Thokala static int keembay_pcie_probe_clocks(struct keembay_pcie *pcie)
1860c87f90bSSrikanth Thokala {
1870c87f90bSSrikanth Thokala struct dw_pcie *pci = &pcie->pci;
1880c87f90bSSrikanth Thokala struct device *dev = pci->dev;
1890c87f90bSSrikanth Thokala
1900c87f90bSSrikanth Thokala pcie->clk_master = keembay_pcie_probe_clock(dev, "master", 0);
1910c87f90bSSrikanth Thokala if (IS_ERR(pcie->clk_master))
1920c87f90bSSrikanth Thokala return dev_err_probe(dev, PTR_ERR(pcie->clk_master),
1930c87f90bSSrikanth Thokala "Failed to enable master clock");
1940c87f90bSSrikanth Thokala
1950c87f90bSSrikanth Thokala pcie->clk_aux = keembay_pcie_probe_clock(dev, "aux", AUX_CLK_RATE_HZ);
1960c87f90bSSrikanth Thokala if (IS_ERR(pcie->clk_aux))
1970c87f90bSSrikanth Thokala return dev_err_probe(dev, PTR_ERR(pcie->clk_aux),
1980c87f90bSSrikanth Thokala "Failed to enable auxiliary clock");
1990c87f90bSSrikanth Thokala
2000c87f90bSSrikanth Thokala return 0;
2010c87f90bSSrikanth Thokala }
2020c87f90bSSrikanth Thokala
2030c87f90bSSrikanth Thokala /*
2040c87f90bSSrikanth Thokala * Initialize the internal PCIe PLL in Host mode.
2050c87f90bSSrikanth Thokala * See the following sections in Keem Bay data book,
2060c87f90bSSrikanth Thokala * (1) 6.4.6.1 PCIe Subsystem Example Initialization,
2070c87f90bSSrikanth Thokala * (2) 6.8 PCIe Low Jitter PLL for Ref Clk Generation.
2080c87f90bSSrikanth Thokala */
keembay_pcie_pll_init(struct keembay_pcie * pcie)2090c87f90bSSrikanth Thokala static int keembay_pcie_pll_init(struct keembay_pcie *pcie)
2100c87f90bSSrikanth Thokala {
2110c87f90bSSrikanth Thokala struct dw_pcie *pci = &pcie->pci;
2120c87f90bSSrikanth Thokala u32 val;
2130c87f90bSSrikanth Thokala int ret;
2140c87f90bSSrikanth Thokala
2150c87f90bSSrikanth Thokala val = FIELD_PREP(LJPLL_REF_DIV, 0) | FIELD_PREP(LJPLL_FB_DIV, 0x32);
2160c87f90bSSrikanth Thokala writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_2);
2170c87f90bSSrikanth Thokala
2180c87f90bSSrikanth Thokala val = FIELD_PREP(LJPLL_POST_DIV3A, 0x2) |
2190c87f90bSSrikanth Thokala FIELD_PREP(LJPLL_POST_DIV2A, 0x2);
2200c87f90bSSrikanth Thokala writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_3);
2210c87f90bSSrikanth Thokala
2220c87f90bSSrikanth Thokala val = FIELD_PREP(LJPLL_EN, 0x1) | FIELD_PREP(LJPLL_FOUT_EN, 0xc);
2230c87f90bSSrikanth Thokala writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_0);
2240c87f90bSSrikanth Thokala
2250c87f90bSSrikanth Thokala ret = readl_poll_timeout(pcie->apb_base + PCIE_REGS_LJPLL_STA,
2260c87f90bSSrikanth Thokala val, val & LJPLL_LOCK, 20,
2270c87f90bSSrikanth Thokala 500 * USEC_PER_MSEC);
2280c87f90bSSrikanth Thokala if (ret)
2290c87f90bSSrikanth Thokala dev_err(pci->dev, "Low jitter PLL is not locked\n");
2300c87f90bSSrikanth Thokala
2310c87f90bSSrikanth Thokala return ret;
2320c87f90bSSrikanth Thokala }
2330c87f90bSSrikanth Thokala
keembay_pcie_msi_irq_handler(struct irq_desc * desc)2340c87f90bSSrikanth Thokala static void keembay_pcie_msi_irq_handler(struct irq_desc *desc)
2350c87f90bSSrikanth Thokala {
2360c87f90bSSrikanth Thokala struct keembay_pcie *pcie = irq_desc_get_handler_data(desc);
2370c87f90bSSrikanth Thokala struct irq_chip *chip = irq_desc_get_chip(desc);
2380c87f90bSSrikanth Thokala u32 val, mask, status;
23960b3c27fSSerge Semin struct dw_pcie_rp *pp;
2400c87f90bSSrikanth Thokala
2410c87f90bSSrikanth Thokala /*
2420c87f90bSSrikanth Thokala * Keem Bay PCIe Controller provides an additional IP logic on top of
2430c87f90bSSrikanth Thokala * standard DWC IP to clear MSI IRQ by writing '1' to the respective
2440c87f90bSSrikanth Thokala * bit of the status register.
2450c87f90bSSrikanth Thokala *
2460c87f90bSSrikanth Thokala * So, a chained irq handler is defined to handle this additional
2470c87f90bSSrikanth Thokala * IP logic.
2480c87f90bSSrikanth Thokala */
2490c87f90bSSrikanth Thokala
2500c87f90bSSrikanth Thokala chained_irq_enter(chip, desc);
2510c87f90bSSrikanth Thokala
2520c87f90bSSrikanth Thokala pp = &pcie->pci.pp;
2530c87f90bSSrikanth Thokala val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
2540c87f90bSSrikanth Thokala mask = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
2550c87f90bSSrikanth Thokala
2560c87f90bSSrikanth Thokala status = val & mask;
2570c87f90bSSrikanth Thokala
2580c87f90bSSrikanth Thokala if (status & MSI_CTRL_INT) {
2590c87f90bSSrikanth Thokala dw_handle_msi_irq(pp);
2600c87f90bSSrikanth Thokala writel(status, pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
2610c87f90bSSrikanth Thokala }
2620c87f90bSSrikanth Thokala
2630c87f90bSSrikanth Thokala chained_irq_exit(chip, desc);
2640c87f90bSSrikanth Thokala }
2650c87f90bSSrikanth Thokala
keembay_pcie_setup_msi_irq(struct keembay_pcie * pcie)2660c87f90bSSrikanth Thokala static int keembay_pcie_setup_msi_irq(struct keembay_pcie *pcie)
2670c87f90bSSrikanth Thokala {
2680c87f90bSSrikanth Thokala struct dw_pcie *pci = &pcie->pci;
2690c87f90bSSrikanth Thokala struct device *dev = pci->dev;
2700c87f90bSSrikanth Thokala struct platform_device *pdev = to_platform_device(dev);
2710c87f90bSSrikanth Thokala int irq;
2720c87f90bSSrikanth Thokala
2730c87f90bSSrikanth Thokala irq = platform_get_irq_byname(pdev, "pcie");
2740c87f90bSSrikanth Thokala if (irq < 0)
2750c87f90bSSrikanth Thokala return irq;
2760c87f90bSSrikanth Thokala
2770c87f90bSSrikanth Thokala irq_set_chained_handler_and_data(irq, keembay_pcie_msi_irq_handler,
2780c87f90bSSrikanth Thokala pcie);
2790c87f90bSSrikanth Thokala
2800c87f90bSSrikanth Thokala return 0;
2810c87f90bSSrikanth Thokala }
2820c87f90bSSrikanth Thokala
keembay_pcie_ep_init(struct dw_pcie_ep * ep)2830c87f90bSSrikanth Thokala static void keembay_pcie_ep_init(struct dw_pcie_ep *ep)
2840c87f90bSSrikanth Thokala {
2850c87f90bSSrikanth Thokala struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
2860c87f90bSSrikanth Thokala struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
2870c87f90bSSrikanth Thokala
2880c87f90bSSrikanth Thokala writel(EDMA_INT_EN, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
2890c87f90bSSrikanth Thokala }
2900c87f90bSSrikanth Thokala
keembay_pcie_ep_raise_irq(struct dw_pcie_ep * ep,u8 func_no,enum pci_epc_irq_type type,u16 interrupt_num)2910c87f90bSSrikanth Thokala static int keembay_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
2920c87f90bSSrikanth Thokala enum pci_epc_irq_type type,
2930c87f90bSSrikanth Thokala u16 interrupt_num)
2940c87f90bSSrikanth Thokala {
2950c87f90bSSrikanth Thokala struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
2960c87f90bSSrikanth Thokala
2970c87f90bSSrikanth Thokala switch (type) {
2980c87f90bSSrikanth Thokala case PCI_EPC_IRQ_LEGACY:
2990c87f90bSSrikanth Thokala /* Legacy interrupts are not supported in Keem Bay */
3000c87f90bSSrikanth Thokala dev_err(pci->dev, "Legacy IRQ is not supported\n");
3010c87f90bSSrikanth Thokala return -EINVAL;
3020c87f90bSSrikanth Thokala case PCI_EPC_IRQ_MSI:
3030c87f90bSSrikanth Thokala return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
3040c87f90bSSrikanth Thokala case PCI_EPC_IRQ_MSIX:
3050c87f90bSSrikanth Thokala return dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num);
3060c87f90bSSrikanth Thokala default:
3070c87f90bSSrikanth Thokala dev_err(pci->dev, "Unknown IRQ type %d\n", type);
3080c87f90bSSrikanth Thokala return -EINVAL;
3090c87f90bSSrikanth Thokala }
3100c87f90bSSrikanth Thokala }
3110c87f90bSSrikanth Thokala
3120c87f90bSSrikanth Thokala static const struct pci_epc_features keembay_pcie_epc_features = {
3130c87f90bSSrikanth Thokala .linkup_notifier = false,
3140c87f90bSSrikanth Thokala .msi_capable = true,
3150c87f90bSSrikanth Thokala .msix_capable = true,
3160c87f90bSSrikanth Thokala .reserved_bar = BIT(BAR_1) | BIT(BAR_3) | BIT(BAR_5),
3170c87f90bSSrikanth Thokala .bar_fixed_64bit = BIT(BAR_0) | BIT(BAR_2) | BIT(BAR_4),
3180c87f90bSSrikanth Thokala .align = SZ_16K,
3190c87f90bSSrikanth Thokala };
3200c87f90bSSrikanth Thokala
3210c87f90bSSrikanth Thokala static const struct pci_epc_features *
keembay_pcie_get_features(struct dw_pcie_ep * ep)3220c87f90bSSrikanth Thokala keembay_pcie_get_features(struct dw_pcie_ep *ep)
3230c87f90bSSrikanth Thokala {
3240c87f90bSSrikanth Thokala return &keembay_pcie_epc_features;
3250c87f90bSSrikanth Thokala }
3260c87f90bSSrikanth Thokala
3270c87f90bSSrikanth Thokala static const struct dw_pcie_ep_ops keembay_pcie_ep_ops = {
3280c87f90bSSrikanth Thokala .ep_init = keembay_pcie_ep_init,
3290c87f90bSSrikanth Thokala .raise_irq = keembay_pcie_ep_raise_irq,
3300c87f90bSSrikanth Thokala .get_features = keembay_pcie_get_features,
3310c87f90bSSrikanth Thokala };
3320c87f90bSSrikanth Thokala
3330c87f90bSSrikanth Thokala static const struct dw_pcie_host_ops keembay_pcie_host_ops = {
3340c87f90bSSrikanth Thokala };
3350c87f90bSSrikanth Thokala
keembay_pcie_add_pcie_port(struct keembay_pcie * pcie,struct platform_device * pdev)3360c87f90bSSrikanth Thokala static int keembay_pcie_add_pcie_port(struct keembay_pcie *pcie,
3370c87f90bSSrikanth Thokala struct platform_device *pdev)
3380c87f90bSSrikanth Thokala {
3390c87f90bSSrikanth Thokala struct dw_pcie *pci = &pcie->pci;
34060b3c27fSSerge Semin struct dw_pcie_rp *pp = &pci->pp;
3410c87f90bSSrikanth Thokala struct device *dev = &pdev->dev;
3420c87f90bSSrikanth Thokala u32 val;
3430c87f90bSSrikanth Thokala int ret;
3440c87f90bSSrikanth Thokala
3450c87f90bSSrikanth Thokala pp->ops = &keembay_pcie_host_ops;
346db388348SDmitry Baryshkov pp->msi_irq[0] = -ENODEV;
3470c87f90bSSrikanth Thokala
3480c87f90bSSrikanth Thokala ret = keembay_pcie_setup_msi_irq(pcie);
3490c87f90bSSrikanth Thokala if (ret)
3500c87f90bSSrikanth Thokala return ret;
3510c87f90bSSrikanth Thokala
3520c87f90bSSrikanth Thokala pcie->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
3530c87f90bSSrikanth Thokala if (IS_ERR(pcie->reset))
3540c87f90bSSrikanth Thokala return PTR_ERR(pcie->reset);
3550c87f90bSSrikanth Thokala
3560c87f90bSSrikanth Thokala ret = keembay_pcie_probe_clocks(pcie);
3570c87f90bSSrikanth Thokala if (ret)
3580c87f90bSSrikanth Thokala return ret;
3590c87f90bSSrikanth Thokala
3600c87f90bSSrikanth Thokala val = readl(pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
3610c87f90bSSrikanth Thokala val |= PHY0_SRAM_BYPASS;
3620c87f90bSSrikanth Thokala writel(val, pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
3630c87f90bSSrikanth Thokala
3640c87f90bSSrikanth Thokala writel(PCIE_DEVICE_TYPE, pcie->apb_base + PCIE_REGS_PCIE_CFG);
3650c87f90bSSrikanth Thokala
3660c87f90bSSrikanth Thokala ret = keembay_pcie_pll_init(pcie);
3670c87f90bSSrikanth Thokala if (ret)
3680c87f90bSSrikanth Thokala return ret;
3690c87f90bSSrikanth Thokala
3700c87f90bSSrikanth Thokala val = readl(pcie->apb_base + PCIE_REGS_PCIE_CFG);
3710c87f90bSSrikanth Thokala writel(val | PCIE_RSTN, pcie->apb_base + PCIE_REGS_PCIE_CFG);
3720c87f90bSSrikanth Thokala keembay_ep_reset_deassert(pcie);
3730c87f90bSSrikanth Thokala
3740c87f90bSSrikanth Thokala ret = dw_pcie_host_init(pp);
3750c87f90bSSrikanth Thokala if (ret) {
3760c87f90bSSrikanth Thokala keembay_ep_reset_assert(pcie);
3770c87f90bSSrikanth Thokala dev_err(dev, "Failed to initialize host: %d\n", ret);
3780c87f90bSSrikanth Thokala return ret;
3790c87f90bSSrikanth Thokala }
3800c87f90bSSrikanth Thokala
3810c87f90bSSrikanth Thokala val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
3820c87f90bSSrikanth Thokala if (IS_ENABLED(CONFIG_PCI_MSI))
3830c87f90bSSrikanth Thokala val |= MSI_CTRL_INT_EN;
3840c87f90bSSrikanth Thokala writel(val, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
3850c87f90bSSrikanth Thokala
3860c87f90bSSrikanth Thokala return 0;
3870c87f90bSSrikanth Thokala }
3880c87f90bSSrikanth Thokala
keembay_pcie_probe(struct platform_device * pdev)3890c87f90bSSrikanth Thokala static int keembay_pcie_probe(struct platform_device *pdev)
3900c87f90bSSrikanth Thokala {
3910c87f90bSSrikanth Thokala const struct keembay_pcie_of_data *data;
3920c87f90bSSrikanth Thokala struct device *dev = &pdev->dev;
3930c87f90bSSrikanth Thokala struct keembay_pcie *pcie;
3940c87f90bSSrikanth Thokala struct dw_pcie *pci;
3950c87f90bSSrikanth Thokala enum dw_pcie_device_mode mode;
3960c87f90bSSrikanth Thokala
3970c87f90bSSrikanth Thokala data = device_get_match_data(dev);
3980c87f90bSSrikanth Thokala if (!data)
3990c87f90bSSrikanth Thokala return -ENODEV;
4000c87f90bSSrikanth Thokala
4010c87f90bSSrikanth Thokala mode = (enum dw_pcie_device_mode)data->mode;
4020c87f90bSSrikanth Thokala
4030c87f90bSSrikanth Thokala pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
4040c87f90bSSrikanth Thokala if (!pcie)
4050c87f90bSSrikanth Thokala return -ENOMEM;
4060c87f90bSSrikanth Thokala
4070c87f90bSSrikanth Thokala pci = &pcie->pci;
4080c87f90bSSrikanth Thokala pci->dev = dev;
4090c87f90bSSrikanth Thokala pci->ops = &keembay_pcie_ops;
4100c87f90bSSrikanth Thokala
4110c87f90bSSrikanth Thokala pcie->mode = mode;
4120c87f90bSSrikanth Thokala
4130c87f90bSSrikanth Thokala pcie->apb_base = devm_platform_ioremap_resource_byname(pdev, "apb");
4140c87f90bSSrikanth Thokala if (IS_ERR(pcie->apb_base))
4150c87f90bSSrikanth Thokala return PTR_ERR(pcie->apb_base);
4160c87f90bSSrikanth Thokala
4170c87f90bSSrikanth Thokala platform_set_drvdata(pdev, pcie);
4180c87f90bSSrikanth Thokala
4190c87f90bSSrikanth Thokala switch (pcie->mode) {
4200c87f90bSSrikanth Thokala case DW_PCIE_RC_TYPE:
4210c87f90bSSrikanth Thokala if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_HOST))
4220c87f90bSSrikanth Thokala return -ENODEV;
4230c87f90bSSrikanth Thokala
4240c87f90bSSrikanth Thokala return keembay_pcie_add_pcie_port(pcie, pdev);
4250c87f90bSSrikanth Thokala case DW_PCIE_EP_TYPE:
4260c87f90bSSrikanth Thokala if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_EP))
4270c87f90bSSrikanth Thokala return -ENODEV;
4280c87f90bSSrikanth Thokala
4290c87f90bSSrikanth Thokala pci->ep.ops = &keembay_pcie_ep_ops;
4300c87f90bSSrikanth Thokala return dw_pcie_ep_init(&pci->ep);
4310c87f90bSSrikanth Thokala default:
4320c87f90bSSrikanth Thokala dev_err(dev, "Invalid device type %d\n", pcie->mode);
4330c87f90bSSrikanth Thokala return -ENODEV;
4340c87f90bSSrikanth Thokala }
4350c87f90bSSrikanth Thokala }
4360c87f90bSSrikanth Thokala
4370c87f90bSSrikanth Thokala static const struct keembay_pcie_of_data keembay_pcie_rc_of_data = {
4380c87f90bSSrikanth Thokala .mode = DW_PCIE_RC_TYPE,
4390c87f90bSSrikanth Thokala };
4400c87f90bSSrikanth Thokala
4410c87f90bSSrikanth Thokala static const struct keembay_pcie_of_data keembay_pcie_ep_of_data = {
4420c87f90bSSrikanth Thokala .mode = DW_PCIE_EP_TYPE,
4430c87f90bSSrikanth Thokala };
4440c87f90bSSrikanth Thokala
4450c87f90bSSrikanth Thokala static const struct of_device_id keembay_pcie_of_match[] = {
4460c87f90bSSrikanth Thokala {
4470c87f90bSSrikanth Thokala .compatible = "intel,keembay-pcie",
4480c87f90bSSrikanth Thokala .data = &keembay_pcie_rc_of_data,
4490c87f90bSSrikanth Thokala },
4500c87f90bSSrikanth Thokala {
4510c87f90bSSrikanth Thokala .compatible = "intel,keembay-pcie-ep",
4520c87f90bSSrikanth Thokala .data = &keembay_pcie_ep_of_data,
4530c87f90bSSrikanth Thokala },
4540c87f90bSSrikanth Thokala {}
4550c87f90bSSrikanth Thokala };
4560c87f90bSSrikanth Thokala
4570c87f90bSSrikanth Thokala static struct platform_driver keembay_pcie_driver = {
4580c87f90bSSrikanth Thokala .driver = {
4590c87f90bSSrikanth Thokala .name = "keembay-pcie",
4600c87f90bSSrikanth Thokala .of_match_table = keembay_pcie_of_match,
4610c87f90bSSrikanth Thokala .suppress_bind_attrs = true,
4620c87f90bSSrikanth Thokala },
4630c87f90bSSrikanth Thokala .probe = keembay_pcie_probe,
4640c87f90bSSrikanth Thokala };
4650c87f90bSSrikanth Thokala builtin_platform_driver(keembay_pcie_driver);
466