0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/init.h>
0012 #include <linux/of_pci.h>
0013 #include <linux/of_platform.h>
0014 #include <linux/of_address.h>
0015 #include <linux/pci.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/resource.h>
0018
0019 #include "pcie-designware.h"
0020
0021 #define to_ls_pcie_ep(x) dev_get_drvdata((x)->dev)
0022
0023 struct ls_pcie_ep_drvdata {
0024 u32 func_offset;
0025 const struct dw_pcie_ep_ops *ops;
0026 const struct dw_pcie_ops *dw_pcie_ops;
0027 };
0028
0029 struct ls_pcie_ep {
0030 struct dw_pcie *pci;
0031 struct pci_epc_features *ls_epc;
0032 const struct ls_pcie_ep_drvdata *drvdata;
0033 };
0034
0035 static const struct pci_epc_features*
0036 ls_pcie_ep_get_features(struct dw_pcie_ep *ep)
0037 {
0038 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0039 struct ls_pcie_ep *pcie = to_ls_pcie_ep(pci);
0040
0041 return pcie->ls_epc;
0042 }
0043
0044 static void ls_pcie_ep_init(struct dw_pcie_ep *ep)
0045 {
0046 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0047 struct ls_pcie_ep *pcie = to_ls_pcie_ep(pci);
0048 struct dw_pcie_ep_func *ep_func;
0049 enum pci_barno bar;
0050
0051 ep_func = dw_pcie_ep_get_func_from_ep(ep, 0);
0052 if (!ep_func)
0053 return;
0054
0055 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
0056 dw_pcie_ep_reset_bar(pci, bar);
0057
0058 pcie->ls_epc->msi_capable = ep_func->msi_cap ? true : false;
0059 pcie->ls_epc->msix_capable = ep_func->msix_cap ? true : false;
0060 }
0061
0062 static int ls_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
0063 enum pci_epc_irq_type type, u16 interrupt_num)
0064 {
0065 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0066
0067 switch (type) {
0068 case PCI_EPC_IRQ_LEGACY:
0069 return dw_pcie_ep_raise_legacy_irq(ep, func_no);
0070 case PCI_EPC_IRQ_MSI:
0071 return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
0072 case PCI_EPC_IRQ_MSIX:
0073 return dw_pcie_ep_raise_msix_irq_doorbell(ep, func_no,
0074 interrupt_num);
0075 default:
0076 dev_err(pci->dev, "UNKNOWN IRQ type\n");
0077 return -EINVAL;
0078 }
0079 }
0080
0081 static unsigned int ls_pcie_ep_func_conf_select(struct dw_pcie_ep *ep,
0082 u8 func_no)
0083 {
0084 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0085 struct ls_pcie_ep *pcie = to_ls_pcie_ep(pci);
0086
0087 WARN_ON(func_no && !pcie->drvdata->func_offset);
0088 return pcie->drvdata->func_offset * func_no;
0089 }
0090
0091 static const struct dw_pcie_ep_ops ls_pcie_ep_ops = {
0092 .ep_init = ls_pcie_ep_init,
0093 .raise_irq = ls_pcie_ep_raise_irq,
0094 .get_features = ls_pcie_ep_get_features,
0095 .func_conf_select = ls_pcie_ep_func_conf_select,
0096 };
0097
0098 static const struct ls_pcie_ep_drvdata ls1_ep_drvdata = {
0099 .ops = &ls_pcie_ep_ops,
0100 };
0101
0102 static const struct ls_pcie_ep_drvdata ls2_ep_drvdata = {
0103 .func_offset = 0x20000,
0104 .ops = &ls_pcie_ep_ops,
0105 };
0106
0107 static const struct ls_pcie_ep_drvdata lx2_ep_drvdata = {
0108 .func_offset = 0x8000,
0109 .ops = &ls_pcie_ep_ops,
0110 };
0111
0112 static const struct of_device_id ls_pcie_ep_of_match[] = {
0113 { .compatible = "fsl,ls1046a-pcie-ep", .data = &ls1_ep_drvdata },
0114 { .compatible = "fsl,ls1088a-pcie-ep", .data = &ls2_ep_drvdata },
0115 { .compatible = "fsl,ls2088a-pcie-ep", .data = &ls2_ep_drvdata },
0116 { .compatible = "fsl,lx2160ar2-pcie-ep", .data = &lx2_ep_drvdata },
0117 { },
0118 };
0119
0120 static int __init ls_pcie_ep_probe(struct platform_device *pdev)
0121 {
0122 struct device *dev = &pdev->dev;
0123 struct dw_pcie *pci;
0124 struct ls_pcie_ep *pcie;
0125 struct pci_epc_features *ls_epc;
0126 struct resource *dbi_base;
0127
0128 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
0129 if (!pcie)
0130 return -ENOMEM;
0131
0132 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
0133 if (!pci)
0134 return -ENOMEM;
0135
0136 ls_epc = devm_kzalloc(dev, sizeof(*ls_epc), GFP_KERNEL);
0137 if (!ls_epc)
0138 return -ENOMEM;
0139
0140 pcie->drvdata = of_device_get_match_data(dev);
0141
0142 pci->dev = dev;
0143 pci->ops = pcie->drvdata->dw_pcie_ops;
0144
0145 ls_epc->bar_fixed_64bit = (1 << BAR_2) | (1 << BAR_4);
0146
0147 pcie->pci = pci;
0148 pcie->ls_epc = ls_epc;
0149
0150 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
0151 pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_base);
0152 if (IS_ERR(pci->dbi_base))
0153 return PTR_ERR(pci->dbi_base);
0154
0155 pci->ep.ops = &ls_pcie_ep_ops;
0156
0157 platform_set_drvdata(pdev, pcie);
0158
0159 return dw_pcie_ep_init(&pci->ep);
0160 }
0161
0162 static struct platform_driver ls_pcie_ep_driver = {
0163 .driver = {
0164 .name = "layerscape-pcie-ep",
0165 .of_match_table = ls_pcie_ep_of_match,
0166 .suppress_bind_attrs = true,
0167 },
0168 };
0169 builtin_platform_driver_probe(ls_pcie_ep_driver, ls_pcie_ep_probe);