Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * PCIe controller driver for Intel Keem Bay
0004  * Copyright (C) 2020 Intel Corporation
0005  */
0006 
0007 #include <linux/bitfield.h>
0008 #include <linux/bits.h>
0009 #include <linux/clk.h>
0010 #include <linux/delay.h>
0011 #include <linux/err.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/init.h>
0014 #include <linux/iopoll.h>
0015 #include <linux/irqchip/chained_irq.h>
0016 #include <linux/kernel.h>
0017 #include <linux/mod_devicetable.h>
0018 #include <linux/pci.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/property.h>
0021 
0022 #include "pcie-designware.h"
0023 
0024 /* PCIE_REGS_APB_SLV Registers */
0025 #define PCIE_REGS_PCIE_CFG      0x0004
0026 #define  PCIE_DEVICE_TYPE       BIT(8)
0027 #define  PCIE_RSTN          BIT(0)
0028 #define PCIE_REGS_PCIE_APP_CNTRL    0x0008
0029 #define  APP_LTSSM_ENABLE       BIT(0)
0030 #define PCIE_REGS_INTERRUPT_ENABLE  0x0028
0031 #define  MSI_CTRL_INT_EN        BIT(8)
0032 #define  EDMA_INT_EN            GENMASK(7, 0)
0033 #define PCIE_REGS_INTERRUPT_STATUS  0x002c
0034 #define  MSI_CTRL_INT           BIT(8)
0035 #define PCIE_REGS_PCIE_SII_PM_STATE 0x00b0
0036 #define  SMLH_LINK_UP           BIT(19)
0037 #define  RDLH_LINK_UP           BIT(8)
0038 #define  PCIE_REGS_PCIE_SII_LINK_UP (SMLH_LINK_UP | RDLH_LINK_UP)
0039 #define PCIE_REGS_PCIE_PHY_CNTL     0x0164
0040 #define  PHY0_SRAM_BYPASS       BIT(8)
0041 #define PCIE_REGS_PCIE_PHY_STAT     0x0168
0042 #define  PHY0_MPLLA_STATE       BIT(1)
0043 #define PCIE_REGS_LJPLL_STA     0x016c
0044 #define  LJPLL_LOCK         BIT(0)
0045 #define PCIE_REGS_LJPLL_CNTRL_0     0x0170
0046 #define  LJPLL_EN           BIT(29)
0047 #define  LJPLL_FOUT_EN          GENMASK(24, 21)
0048 #define PCIE_REGS_LJPLL_CNTRL_2     0x0178
0049 #define  LJPLL_REF_DIV          GENMASK(17, 12)
0050 #define  LJPLL_FB_DIV           GENMASK(11, 0)
0051 #define PCIE_REGS_LJPLL_CNTRL_3     0x017c
0052 #define  LJPLL_POST_DIV3A       GENMASK(24, 22)
0053 #define  LJPLL_POST_DIV2A       GENMASK(18, 16)
0054 
0055 #define PERST_DELAY_US      1000
0056 #define AUX_CLK_RATE_HZ     24000000
0057 
0058 struct keembay_pcie {
0059     struct dw_pcie      pci;
0060     void __iomem        *apb_base;
0061     enum dw_pcie_device_mode mode;
0062 
0063     struct clk      *clk_master;
0064     struct clk      *clk_aux;
0065     struct gpio_desc    *reset;
0066 };
0067 
0068 struct keembay_pcie_of_data {
0069     enum dw_pcie_device_mode mode;
0070 };
0071 
0072 static void keembay_ep_reset_assert(struct keembay_pcie *pcie)
0073 {
0074     gpiod_set_value_cansleep(pcie->reset, 1);
0075     usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
0076 }
0077 
0078 static void keembay_ep_reset_deassert(struct keembay_pcie *pcie)
0079 {
0080     /*
0081      * Ensure that PERST# is asserted for a minimum of 100ms.
0082      *
0083      * For more details, refer to PCI Express Card Electromechanical
0084      * Specification Revision 1.1, Table-2.4.
0085      */
0086     msleep(100);
0087 
0088     gpiod_set_value_cansleep(pcie->reset, 0);
0089     usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
0090 }
0091 
0092 static void keembay_pcie_ltssm_set(struct keembay_pcie *pcie, bool enable)
0093 {
0094     u32 val;
0095 
0096     val = readl(pcie->apb_base + PCIE_REGS_PCIE_APP_CNTRL);
0097     if (enable)
0098         val |= APP_LTSSM_ENABLE;
0099     else
0100         val &= ~APP_LTSSM_ENABLE;
0101     writel(val, pcie->apb_base + PCIE_REGS_PCIE_APP_CNTRL);
0102 }
0103 
0104 static int keembay_pcie_link_up(struct dw_pcie *pci)
0105 {
0106     struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
0107     u32 val;
0108 
0109     val = readl(pcie->apb_base + PCIE_REGS_PCIE_SII_PM_STATE);
0110 
0111     return (val & PCIE_REGS_PCIE_SII_LINK_UP) == PCIE_REGS_PCIE_SII_LINK_UP;
0112 }
0113 
0114 static int keembay_pcie_start_link(struct dw_pcie *pci)
0115 {
0116     struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
0117     u32 val;
0118     int ret;
0119 
0120     if (pcie->mode == DW_PCIE_EP_TYPE)
0121         return 0;
0122 
0123     keembay_pcie_ltssm_set(pcie, false);
0124 
0125     ret = readl_poll_timeout(pcie->apb_base + PCIE_REGS_PCIE_PHY_STAT,
0126                  val, val & PHY0_MPLLA_STATE, 20,
0127                  500 * USEC_PER_MSEC);
0128     if (ret) {
0129         dev_err(pci->dev, "MPLLA is not locked\n");
0130         return ret;
0131     }
0132 
0133     keembay_pcie_ltssm_set(pcie, true);
0134 
0135     return 0;
0136 }
0137 
0138 static void keembay_pcie_stop_link(struct dw_pcie *pci)
0139 {
0140     struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
0141 
0142     keembay_pcie_ltssm_set(pcie, false);
0143 }
0144 
0145 static const struct dw_pcie_ops keembay_pcie_ops = {
0146     .link_up    = keembay_pcie_link_up,
0147     .start_link = keembay_pcie_start_link,
0148     .stop_link  = keembay_pcie_stop_link,
0149 };
0150 
0151 static inline struct clk *keembay_pcie_probe_clock(struct device *dev,
0152                            const char *id, u64 rate)
0153 {
0154     struct clk *clk;
0155     int ret;
0156 
0157     clk = devm_clk_get(dev, id);
0158     if (IS_ERR(clk))
0159         return clk;
0160 
0161     if (rate) {
0162         ret = clk_set_rate(clk, rate);
0163         if (ret)
0164             return ERR_PTR(ret);
0165     }
0166 
0167     ret = clk_prepare_enable(clk);
0168     if (ret)
0169         return ERR_PTR(ret);
0170 
0171     ret = devm_add_action_or_reset(dev,
0172                        (void(*)(void *))clk_disable_unprepare,
0173                        clk);
0174     if (ret)
0175         return ERR_PTR(ret);
0176 
0177     return clk;
0178 }
0179 
0180 static int keembay_pcie_probe_clocks(struct keembay_pcie *pcie)
0181 {
0182     struct dw_pcie *pci = &pcie->pci;
0183     struct device *dev = pci->dev;
0184 
0185     pcie->clk_master = keembay_pcie_probe_clock(dev, "master", 0);
0186     if (IS_ERR(pcie->clk_master))
0187         return dev_err_probe(dev, PTR_ERR(pcie->clk_master),
0188                      "Failed to enable master clock");
0189 
0190     pcie->clk_aux = keembay_pcie_probe_clock(dev, "aux", AUX_CLK_RATE_HZ);
0191     if (IS_ERR(pcie->clk_aux))
0192         return dev_err_probe(dev, PTR_ERR(pcie->clk_aux),
0193                      "Failed to enable auxiliary clock");
0194 
0195     return 0;
0196 }
0197 
0198 /*
0199  * Initialize the internal PCIe PLL in Host mode.
0200  * See the following sections in Keem Bay data book,
0201  * (1) 6.4.6.1 PCIe Subsystem Example Initialization,
0202  * (2) 6.8 PCIe Low Jitter PLL for Ref Clk Generation.
0203  */
0204 static int keembay_pcie_pll_init(struct keembay_pcie *pcie)
0205 {
0206     struct dw_pcie *pci = &pcie->pci;
0207     u32 val;
0208     int ret;
0209 
0210     val = FIELD_PREP(LJPLL_REF_DIV, 0) | FIELD_PREP(LJPLL_FB_DIV, 0x32);
0211     writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_2);
0212 
0213     val = FIELD_PREP(LJPLL_POST_DIV3A, 0x2) |
0214         FIELD_PREP(LJPLL_POST_DIV2A, 0x2);
0215     writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_3);
0216 
0217     val = FIELD_PREP(LJPLL_EN, 0x1) | FIELD_PREP(LJPLL_FOUT_EN, 0xc);
0218     writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_0);
0219 
0220     ret = readl_poll_timeout(pcie->apb_base + PCIE_REGS_LJPLL_STA,
0221                  val, val & LJPLL_LOCK, 20,
0222                  500 * USEC_PER_MSEC);
0223     if (ret)
0224         dev_err(pci->dev, "Low jitter PLL is not locked\n");
0225 
0226     return ret;
0227 }
0228 
0229 static void keembay_pcie_msi_irq_handler(struct irq_desc *desc)
0230 {
0231     struct keembay_pcie *pcie = irq_desc_get_handler_data(desc);
0232     struct irq_chip *chip = irq_desc_get_chip(desc);
0233     u32 val, mask, status;
0234     struct dw_pcie_rp *pp;
0235 
0236     /*
0237      * Keem Bay PCIe Controller provides an additional IP logic on top of
0238      * standard DWC IP to clear MSI IRQ by writing '1' to the respective
0239      * bit of the status register.
0240      *
0241      * So, a chained irq handler is defined to handle this additional
0242      * IP logic.
0243      */
0244 
0245     chained_irq_enter(chip, desc);
0246 
0247     pp = &pcie->pci.pp;
0248     val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
0249     mask = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
0250 
0251     status = val & mask;
0252 
0253     if (status & MSI_CTRL_INT) {
0254         dw_handle_msi_irq(pp);
0255         writel(status, pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
0256     }
0257 
0258     chained_irq_exit(chip, desc);
0259 }
0260 
0261 static int keembay_pcie_setup_msi_irq(struct keembay_pcie *pcie)
0262 {
0263     struct dw_pcie *pci = &pcie->pci;
0264     struct device *dev = pci->dev;
0265     struct platform_device *pdev = to_platform_device(dev);
0266     int irq;
0267 
0268     irq = platform_get_irq_byname(pdev, "pcie");
0269     if (irq < 0)
0270         return irq;
0271 
0272     irq_set_chained_handler_and_data(irq, keembay_pcie_msi_irq_handler,
0273                      pcie);
0274 
0275     return 0;
0276 }
0277 
0278 static void keembay_pcie_ep_init(struct dw_pcie_ep *ep)
0279 {
0280     struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0281     struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
0282 
0283     writel(EDMA_INT_EN, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
0284 }
0285 
0286 static int keembay_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
0287                      enum pci_epc_irq_type type,
0288                      u16 interrupt_num)
0289 {
0290     struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0291 
0292     switch (type) {
0293     case PCI_EPC_IRQ_LEGACY:
0294         /* Legacy interrupts are not supported in Keem Bay */
0295         dev_err(pci->dev, "Legacy IRQ is not supported\n");
0296         return -EINVAL;
0297     case PCI_EPC_IRQ_MSI:
0298         return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
0299     case PCI_EPC_IRQ_MSIX:
0300         return dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num);
0301     default:
0302         dev_err(pci->dev, "Unknown IRQ type %d\n", type);
0303         return -EINVAL;
0304     }
0305 }
0306 
0307 static const struct pci_epc_features keembay_pcie_epc_features = {
0308     .linkup_notifier    = false,
0309     .msi_capable        = true,
0310     .msix_capable       = true,
0311     .reserved_bar       = BIT(BAR_1) | BIT(BAR_3) | BIT(BAR_5),
0312     .bar_fixed_64bit    = BIT(BAR_0) | BIT(BAR_2) | BIT(BAR_4),
0313     .align          = SZ_16K,
0314 };
0315 
0316 static const struct pci_epc_features *
0317 keembay_pcie_get_features(struct dw_pcie_ep *ep)
0318 {
0319     return &keembay_pcie_epc_features;
0320 }
0321 
0322 static const struct dw_pcie_ep_ops keembay_pcie_ep_ops = {
0323     .ep_init    = keembay_pcie_ep_init,
0324     .raise_irq  = keembay_pcie_ep_raise_irq,
0325     .get_features   = keembay_pcie_get_features,
0326 };
0327 
0328 static const struct dw_pcie_host_ops keembay_pcie_host_ops = {
0329 };
0330 
0331 static int keembay_pcie_add_pcie_port(struct keembay_pcie *pcie,
0332                       struct platform_device *pdev)
0333 {
0334     struct dw_pcie *pci = &pcie->pci;
0335     struct dw_pcie_rp *pp = &pci->pp;
0336     struct device *dev = &pdev->dev;
0337     u32 val;
0338     int ret;
0339 
0340     pp->ops = &keembay_pcie_host_ops;
0341     pp->msi_irq[0] = -ENODEV;
0342 
0343     ret = keembay_pcie_setup_msi_irq(pcie);
0344     if (ret)
0345         return ret;
0346 
0347     pcie->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
0348     if (IS_ERR(pcie->reset))
0349         return PTR_ERR(pcie->reset);
0350 
0351     ret = keembay_pcie_probe_clocks(pcie);
0352     if (ret)
0353         return ret;
0354 
0355     val = readl(pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
0356     val |= PHY0_SRAM_BYPASS;
0357     writel(val, pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
0358 
0359     writel(PCIE_DEVICE_TYPE, pcie->apb_base + PCIE_REGS_PCIE_CFG);
0360 
0361     ret = keembay_pcie_pll_init(pcie);
0362     if (ret)
0363         return ret;
0364 
0365     val = readl(pcie->apb_base + PCIE_REGS_PCIE_CFG);
0366     writel(val | PCIE_RSTN, pcie->apb_base + PCIE_REGS_PCIE_CFG);
0367     keembay_ep_reset_deassert(pcie);
0368 
0369     ret = dw_pcie_host_init(pp);
0370     if (ret) {
0371         keembay_ep_reset_assert(pcie);
0372         dev_err(dev, "Failed to initialize host: %d\n", ret);
0373         return ret;
0374     }
0375 
0376     val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
0377     if (IS_ENABLED(CONFIG_PCI_MSI))
0378         val |= MSI_CTRL_INT_EN;
0379     writel(val, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
0380 
0381     return 0;
0382 }
0383 
0384 static int keembay_pcie_probe(struct platform_device *pdev)
0385 {
0386     const struct keembay_pcie_of_data *data;
0387     struct device *dev = &pdev->dev;
0388     struct keembay_pcie *pcie;
0389     struct dw_pcie *pci;
0390     enum dw_pcie_device_mode mode;
0391 
0392     data = device_get_match_data(dev);
0393     if (!data)
0394         return -ENODEV;
0395 
0396     mode = (enum dw_pcie_device_mode)data->mode;
0397 
0398     pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
0399     if (!pcie)
0400         return -ENOMEM;
0401 
0402     pci = &pcie->pci;
0403     pci->dev = dev;
0404     pci->ops = &keembay_pcie_ops;
0405 
0406     pcie->mode = mode;
0407 
0408     pcie->apb_base = devm_platform_ioremap_resource_byname(pdev, "apb");
0409     if (IS_ERR(pcie->apb_base))
0410         return PTR_ERR(pcie->apb_base);
0411 
0412     platform_set_drvdata(pdev, pcie);
0413 
0414     switch (pcie->mode) {
0415     case DW_PCIE_RC_TYPE:
0416         if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_HOST))
0417             return -ENODEV;
0418 
0419         return keembay_pcie_add_pcie_port(pcie, pdev);
0420     case DW_PCIE_EP_TYPE:
0421         if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_EP))
0422             return -ENODEV;
0423 
0424         pci->ep.ops = &keembay_pcie_ep_ops;
0425         return dw_pcie_ep_init(&pci->ep);
0426     default:
0427         dev_err(dev, "Invalid device type %d\n", pcie->mode);
0428         return -ENODEV;
0429     }
0430 }
0431 
0432 static const struct keembay_pcie_of_data keembay_pcie_rc_of_data = {
0433     .mode = DW_PCIE_RC_TYPE,
0434 };
0435 
0436 static const struct keembay_pcie_of_data keembay_pcie_ep_of_data = {
0437     .mode = DW_PCIE_EP_TYPE,
0438 };
0439 
0440 static const struct of_device_id keembay_pcie_of_match[] = {
0441     {
0442         .compatible = "intel,keembay-pcie",
0443         .data = &keembay_pcie_rc_of_data,
0444     },
0445     {
0446         .compatible = "intel,keembay-pcie-ep",
0447         .data = &keembay_pcie_ep_of_data,
0448     },
0449     {}
0450 };
0451 
0452 static struct platform_driver keembay_pcie_driver = {
0453     .driver = {
0454         .name = "keembay-pcie",
0455         .of_match_table = keembay_pcie_of_match,
0456         .suppress_bind_attrs = true,
0457     },
0458     .probe  = keembay_pcie_probe,
0459 };
0460 builtin_platform_driver(keembay_pcie_driver);