blob: 348f9c5e0433e229737eb53034ed7a0ac94321f6 [file] [log] [blame]
Pratyush Anand51b66a62014-02-11 11:39:26 +05301/*
2 * PCIe host controller driver for ST Microelectronics SPEAr13xx SoCs
3 *
4 * SPEAr13xx PCIe Glue Layer Source Code
5 *
6 * Copyright (C) 2010-2014 ST Microelectronics
Pratyush Anande34cadd2015-06-25 15:01:08 -07007 * Pratyush Anand <pratyush.anand@gmail.com>
Pratyush Anand9c5dcdd2015-06-25 15:01:11 -07008 * Mohit Kumar <mohit.kumar.dhaka@gmail.com>
Pratyush Anand51b66a62014-02-11 11:39:26 +05309 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14
15#include <linux/clk.h>
Pratyush Anand51b66a62014-02-11 11:39:26 +053016#include <linux/interrupt.h>
17#include <linux/kernel.h>
Paul Gortmaker56540c72016-08-22 17:59:45 -040018#include <linux/init.h>
Pratyush Anand51b66a62014-02-11 11:39:26 +053019#include <linux/of.h>
20#include <linux/pci.h>
21#include <linux/phy/phy.h>
22#include <linux/platform_device.h>
23#include <linux/resource.h>
24
25#include "pcie-designware.h"
26
27struct spear13xx_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053028 struct dw_pcie *pci;
Pratyush Anand51b66a62014-02-11 11:39:26 +053029 void __iomem *app_base;
30 struct phy *phy;
31 struct clk *clk;
Pratyush Anand51b66a62014-02-11 11:39:26 +053032 bool is_gen1;
33};
34
35struct pcie_app_reg {
36 u32 app_ctrl_0; /* cr0 */
37 u32 app_ctrl_1; /* cr1 */
38 u32 app_status_0; /* cr2 */
39 u32 app_status_1; /* cr3 */
40 u32 msg_status; /* cr4 */
41 u32 msg_payload; /* cr5 */
42 u32 int_sts; /* cr6 */
43 u32 int_clr; /* cr7 */
44 u32 int_mask; /* cr8 */
45 u32 mst_bmisc; /* cr9 */
46 u32 phy_ctrl; /* cr10 */
47 u32 phy_status; /* cr11 */
48 u32 cxpl_debug_info_0; /* cr12 */
49 u32 cxpl_debug_info_1; /* cr13 */
50 u32 ven_msg_ctrl_0; /* cr14 */
51 u32 ven_msg_ctrl_1; /* cr15 */
52 u32 ven_msg_data_0; /* cr16 */
53 u32 ven_msg_data_1; /* cr17 */
54 u32 ven_msi_0; /* cr18 */
55 u32 ven_msi_1; /* cr19 */
56 u32 mst_rmisc; /* cr20 */
57};
58
59/* CR0 ID */
Pratyush Anand51b66a62014-02-11 11:39:26 +053060#define APP_LTSSM_ENABLE_ID 3
Pratyush Anand51b66a62014-02-11 11:39:26 +053061#define DEVICE_TYPE_RC (4 << 25)
Pratyush Anand51b66a62014-02-11 11:39:26 +053062#define MISCTRL_EN_ID 30
63#define REG_TRANSLATION_ENABLE 31
64
Pratyush Anand51b66a62014-02-11 11:39:26 +053065/* CR3 ID */
Pratyush Anand51b66a62014-02-11 11:39:26 +053066#define XMLH_LINK_UP (1 << 6)
67
Pratyush Anand51b66a62014-02-11 11:39:26 +053068/* CR6 */
Pratyush Anand51b66a62014-02-11 11:39:26 +053069#define MSI_CTRL_INT (1 << 26)
70
Pratyush Anand51b66a62014-02-11 11:39:26 +053071#define EXP_CAP_ID_OFFSET 0x70
72
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053073#define to_spear13xx_pcie(x) dev_get_drvdata((x)->dev)
Pratyush Anand51b66a62014-02-11 11:39:26 +053074
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -050075static int spear13xx_pcie_establish_link(struct spear13xx_pcie *spear13xx_pcie)
Pratyush Anand51b66a62014-02-11 11:39:26 +053076{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053077 struct dw_pcie *pci = spear13xx_pcie->pci;
78 struct pcie_port *pp = &pci->pp;
Pratyush Anand51b66a62014-02-11 11:39:26 +053079 struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -050080 u32 val;
Pratyush Anand51b66a62014-02-11 11:39:26 +053081 u32 exp_cap_off = EXP_CAP_ID_OFFSET;
82
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053083 if (dw_pcie_link_up(pci)) {
84 dev_err(pci->dev, "link already up\n");
Pratyush Anand51b66a62014-02-11 11:39:26 +053085 return 0;
86 }
87
88 dw_pcie_setup_rc(pp);
89
90 /*
91 * this controller support only 128 bytes read size, however its
92 * default value in capability register is 512 bytes. So force
93 * it to 128 here.
94 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053095 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, &val);
Pratyush Anand51b66a62014-02-11 11:39:26 +053096 val &= ~PCI_EXP_DEVCTL_READRQ;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053097 dw_pcie_write(pci->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, val);
Pratyush Anand51b66a62014-02-11 11:39:26 +053098
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053099 dw_pcie_write(pci->dbi_base + PCI_VENDOR_ID, 2, 0x104A);
100 dw_pcie_write(pci->dbi_base + PCI_DEVICE_ID, 2, 0xCD80);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530101
102 /*
103 * if is_gen1 is set then handle it, so that some buggy card
104 * also works
105 */
106 if (spear13xx_pcie->is_gen1) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530107 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530108 4, &val);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530109 if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
110 val &= ~((u32)PCI_EXP_LNKCAP_SLS);
111 val |= PCI_EXP_LNKCAP_SLS_2_5GB;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530112 dw_pcie_write(pci->dbi_base + exp_cap_off +
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530113 PCI_EXP_LNKCAP, 4, val);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530114 }
115
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530116 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530117 2, &val);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530118 if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
119 val &= ~((u32)PCI_EXP_LNKCAP_SLS);
120 val |= PCI_EXP_LNKCAP_SLS_2_5GB;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530121 dw_pcie_write(pci->dbi_base + exp_cap_off +
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530122 PCI_EXP_LNKCTL2, 2, val);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530123 }
124 }
125
126 /* enable ltssm */
127 writel(DEVICE_TYPE_RC | (1 << MISCTRL_EN_ID)
128 | (1 << APP_LTSSM_ENABLE_ID)
129 | ((u32)1 << REG_TRANSLATION_ENABLE),
130 &app_reg->app_ctrl_0);
131
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530132 return dw_pcie_wait_for_link(pci);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530133}
134
135static irqreturn_t spear13xx_pcie_irq_handler(int irq, void *arg)
136{
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -0500137 struct spear13xx_pcie *spear13xx_pcie = arg;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530138 struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530139 struct dw_pcie *pci = spear13xx_pcie->pci;
140 struct pcie_port *pp = &pci->pp;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530141 unsigned int status;
142
143 status = readl(&app_reg->int_sts);
144
145 if (status & MSI_CTRL_INT) {
Fabio Estevam68ebb7c2015-08-20 01:31:24 -0500146 BUG_ON(!IS_ENABLED(CONFIG_PCI_MSI));
Pratyush Anand51b66a62014-02-11 11:39:26 +0530147 dw_handle_msi_irq(pp);
148 }
149
150 writel(status, &app_reg->int_clr);
151
152 return IRQ_HANDLED;
153}
154
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -0500155static void spear13xx_pcie_enable_interrupts(struct spear13xx_pcie *spear13xx_pcie)
Pratyush Anand51b66a62014-02-11 11:39:26 +0530156{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530157 struct dw_pcie *pci = spear13xx_pcie->pci;
158 struct pcie_port *pp = &pci->pp;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530159 struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
160
161 /* Enable MSI interrupt */
162 if (IS_ENABLED(CONFIG_PCI_MSI)) {
163 dw_pcie_msi_init(pp);
164 writel(readl(&app_reg->int_mask) |
165 MSI_CTRL_INT, &app_reg->int_mask);
166 }
167}
168
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530169static int spear13xx_pcie_link_up(struct dw_pcie *pci)
Pratyush Anand51b66a62014-02-11 11:39:26 +0530170{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530171 struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530172 struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
173
174 if (readl(&app_reg->app_status_1) & XMLH_LINK_UP)
175 return 1;
176
177 return 0;
178}
179
180static void spear13xx_pcie_host_init(struct pcie_port *pp)
181{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530182 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
183 struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -0500184
185 spear13xx_pcie_establish_link(spear13xx_pcie);
186 spear13xx_pcie_enable_interrupts(spear13xx_pcie);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530187}
188
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530189static struct dw_pcie_host_ops spear13xx_pcie_host_ops = {
Pratyush Anand51b66a62014-02-11 11:39:26 +0530190 .host_init = spear13xx_pcie_host_init,
191};
192
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -0500193static int spear13xx_add_pcie_port(struct spear13xx_pcie *spear13xx_pcie,
194 struct platform_device *pdev)
Pratyush Anand51b66a62014-02-11 11:39:26 +0530195{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530196 struct dw_pcie *pci = spear13xx_pcie->pci;
197 struct pcie_port *pp = &pci->pp;
198 struct device *dev = &pdev->dev;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530199 int ret;
200
201 pp->irq = platform_get_irq(pdev, 0);
202 if (!pp->irq) {
203 dev_err(dev, "failed to get irq\n");
204 return -ENODEV;
205 }
206 ret = devm_request_irq(dev, pp->irq, spear13xx_pcie_irq_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200207 IRQF_SHARED | IRQF_NO_THREAD,
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -0500208 "spear1340-pcie", spear13xx_pcie);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530209 if (ret) {
210 dev_err(dev, "failed to request irq %d\n", pp->irq);
211 return ret;
212 }
213
214 pp->root_bus_nr = -1;
215 pp->ops = &spear13xx_pcie_host_ops;
216
217 ret = dw_pcie_host_init(pp);
218 if (ret) {
219 dev_err(dev, "failed to initialize host\n");
220 return ret;
221 }
222
223 return 0;
224}
225
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530226static const struct dw_pcie_ops dw_pcie_ops = {
227 .link_up = spear13xx_pcie_link_up,
228};
229
Matwey V. Kornilova43f32d2015-02-19 20:41:48 +0300230static int spear13xx_pcie_probe(struct platform_device *pdev)
Pratyush Anand51b66a62014-02-11 11:39:26 +0530231{
Bjorn Helgaas6a43a422016-10-06 13:42:09 -0500232 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530233 struct dw_pcie *pci;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530234 struct spear13xx_pcie *spear13xx_pcie;
Bjorn Helgaas6a43a422016-10-06 13:42:09 -0500235 struct device_node *np = dev->of_node;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530236 struct resource *dbi_base;
237 int ret;
238
239 spear13xx_pcie = devm_kzalloc(dev, sizeof(*spear13xx_pcie), GFP_KERNEL);
Jingoo Han20f9ece2014-11-12 12:29:02 +0900240 if (!spear13xx_pcie)
Pratyush Anand51b66a62014-02-11 11:39:26 +0530241 return -ENOMEM;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530242
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530243 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
244 if (!pci)
245 return -ENOMEM;
246
247 pci->dev = dev;
248 pci->ops = &dw_pcie_ops;
249
Pratyush Anand51b66a62014-02-11 11:39:26 +0530250 spear13xx_pcie->phy = devm_phy_get(dev, "pcie-phy");
251 if (IS_ERR(spear13xx_pcie->phy)) {
252 ret = PTR_ERR(spear13xx_pcie->phy);
253 if (ret == -EPROBE_DEFER)
254 dev_info(dev, "probe deferred\n");
255 else
256 dev_err(dev, "couldn't get pcie-phy\n");
257 return ret;
258 }
259
260 phy_init(spear13xx_pcie->phy);
261
262 spear13xx_pcie->clk = devm_clk_get(dev, NULL);
263 if (IS_ERR(spear13xx_pcie->clk)) {
264 dev_err(dev, "couldn't get clk for pcie\n");
265 return PTR_ERR(spear13xx_pcie->clk);
266 }
267 ret = clk_prepare_enable(spear13xx_pcie->clk);
268 if (ret) {
269 dev_err(dev, "couldn't enable clk for pcie\n");
270 return ret;
271 }
272
Pratyush Anand65aaae22014-09-03 10:50:49 +0530273 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530274 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
275 if (IS_ERR(pci->dbi_base)) {
Pratyush Anand51b66a62014-02-11 11:39:26 +0530276 dev_err(dev, "couldn't remap dbi base %p\n", dbi_base);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530277 ret = PTR_ERR(pci->dbi_base);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530278 goto fail_clk;
279 }
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530280 spear13xx_pcie->app_base = pci->dbi_base + 0x2000;
Pratyush Anand51b66a62014-02-11 11:39:26 +0530281
282 if (of_property_read_bool(np, "st,pcie-is-gen1"))
283 spear13xx_pcie->is_gen1 = true;
284
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530285 platform_set_drvdata(pdev, spear13xx_pcie);
286
Bjorn Helgaasffe82fa2016-10-06 13:42:08 -0500287 ret = spear13xx_add_pcie_port(spear13xx_pcie, pdev);
Pratyush Anand51b66a62014-02-11 11:39:26 +0530288 if (ret < 0)
289 goto fail_clk;
290
Pratyush Anand51b66a62014-02-11 11:39:26 +0530291 return 0;
292
293fail_clk:
294 clk_disable_unprepare(spear13xx_pcie->clk);
295
296 return ret;
297}
298
Pratyush Anand51b66a62014-02-11 11:39:26 +0530299static const struct of_device_id spear13xx_pcie_of_match[] = {
300 { .compatible = "st,spear1340-pcie", },
301 {},
302};
Pratyush Anand51b66a62014-02-11 11:39:26 +0530303
Matwey V. Kornilova43f32d2015-02-19 20:41:48 +0300304static struct platform_driver spear13xx_pcie_driver = {
Pratyush Anand51b66a62014-02-11 11:39:26 +0530305 .probe = spear13xx_pcie_probe,
Pratyush Anand51b66a62014-02-11 11:39:26 +0530306 .driver = {
307 .name = "spear-pcie",
Pratyush Anand51b66a62014-02-11 11:39:26 +0530308 .of_match_table = of_match_ptr(spear13xx_pcie_of_match),
309 },
310};
311
Geliang Tang554d9ec2016-11-23 22:55:07 +0800312builtin_platform_driver(spear13xx_pcie_driver);