blob: 0ac0f18690f20a6743193cb9714feaf72f309487 [file] [log] [blame]
Thomas Petazzoni1c52a512016-04-26 10:31:46 +02001/*
2 * PCIe host controller driver for Marvell Armada-8K SoCs
3 *
4 * Armada-8K PCIe Glue Layer Source Code
5 *
6 * Copyright (C) 2016 Marvell Technology Group Ltd.
7 *
Paul Gortmaker0e6f98c2016-07-02 19:13:21 -04008 * Author: Yehuda Yitshak <yehuday@marvell.com>
9 * Author: Shadi Ammouri <shadi@marvell.com>
10 *
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020011 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
16#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
Paul Gortmaker0e6f98c2016-07-02 19:13:21 -040020#include <linux/init.h>
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020021#include <linux/of.h>
22#include <linux/pci.h>
23#include <linux/phy/phy.h>
24#include <linux/platform_device.h>
25#include <linux/resource.h>
26#include <linux/of_pci.h>
27#include <linux/of_irq.h>
28
29#include "pcie-designware.h"
30
31struct armada8k_pcie {
Bjorn Helgaasa4778152016-10-06 13:30:00 -050032 struct pcie_port pp; /* pp.dbi_base is DT ctrl */
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020033 struct clk *clk;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020034};
35
36#define PCIE_VENDOR_REGS_OFFSET 0x8000
37
Bjorn Helgaas74e69072016-10-06 13:29:59 -050038#define PCIE_GLOBAL_CONTROL_REG (PCIE_VENDOR_REGS_OFFSET + 0x0)
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020039#define PCIE_APP_LTSSM_EN BIT(2)
40#define PCIE_DEVICE_TYPE_SHIFT 4
41#define PCIE_DEVICE_TYPE_MASK 0xF
42#define PCIE_DEVICE_TYPE_RC 0x4 /* Root complex */
43
Bjorn Helgaas74e69072016-10-06 13:29:59 -050044#define PCIE_GLOBAL_STATUS_REG (PCIE_VENDOR_REGS_OFFSET + 0x8)
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020045#define PCIE_GLB_STS_RDLH_LINK_UP BIT(1)
46#define PCIE_GLB_STS_PHY_LINK_UP BIT(9)
47
Bjorn Helgaas74e69072016-10-06 13:29:59 -050048#define PCIE_GLOBAL_INT_CAUSE1_REG (PCIE_VENDOR_REGS_OFFSET + 0x1C)
49#define PCIE_GLOBAL_INT_MASK1_REG (PCIE_VENDOR_REGS_OFFSET + 0x20)
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020050#define PCIE_INT_A_ASSERT_MASK BIT(9)
51#define PCIE_INT_B_ASSERT_MASK BIT(10)
52#define PCIE_INT_C_ASSERT_MASK BIT(11)
53#define PCIE_INT_D_ASSERT_MASK BIT(12)
54
Bjorn Helgaas74e69072016-10-06 13:29:59 -050055#define PCIE_ARCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x50)
56#define PCIE_AWCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x54)
57#define PCIE_ARUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x5C)
58#define PCIE_AWUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x60)
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020059/*
60 * AR/AW Cache defauls: Normal memory, Write-Back, Read / Write
61 * allocate
62 */
63#define ARCACHE_DEFAULT_VALUE 0x3511
64#define AWCACHE_DEFAULT_VALUE 0x5311
65
66#define DOMAIN_OUTER_SHAREABLE 0x2
67#define AX_USER_DOMAIN_MASK 0x3
68#define AX_USER_DOMAIN_SHIFT 4
69
70#define to_armada8k_pcie(x) container_of(x, struct armada8k_pcie, pp)
71
72static int armada8k_pcie_link_up(struct pcie_port *pp)
73{
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020074 u32 reg;
75 u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
76
Bjorn Helgaas76876952016-10-11 20:15:11 -050077 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_STATUS_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020078
79 if ((reg & mask) == mask)
80 return 1;
81
82 dev_dbg(pp->dev, "No link detected (Global-Status: 0x%08x).\n", reg);
83 return 0;
84}
85
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -050086static void armada8k_pcie_establish_link(struct armada8k_pcie *pcie)
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020087{
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -050088 struct pcie_port *pp = &pcie->pp;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020089 u32 reg;
90
91 if (!dw_pcie_link_up(pp)) {
92 /* Disable LTSSM state machine to enable configuration */
Bjorn Helgaas76876952016-10-11 20:15:11 -050093 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020094 reg &= ~(PCIE_APP_LTSSM_EN);
Bjorn Helgaas76876952016-10-11 20:15:11 -050095 dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020096 }
97
98 /* Set the device to root complex mode */
Bjorn Helgaas76876952016-10-11 20:15:11 -050099 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200100 reg &= ~(PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_SHIFT);
101 reg |= PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_SHIFT;
Bjorn Helgaas76876952016-10-11 20:15:11 -0500102 dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200103
104 /* Set the PCIe master AxCache attributes */
Bjorn Helgaas76876952016-10-11 20:15:11 -0500105 dw_pcie_writel_rc(pp, PCIE_ARCACHE_TRC_REG, ARCACHE_DEFAULT_VALUE);
106 dw_pcie_writel_rc(pp, PCIE_AWCACHE_TRC_REG, AWCACHE_DEFAULT_VALUE);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200107
108 /* Set the PCIe master AxDomain attributes */
Bjorn Helgaas76876952016-10-11 20:15:11 -0500109 reg = dw_pcie_readl_rc(pp, PCIE_ARUSER_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200110 reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
111 reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
Bjorn Helgaas76876952016-10-11 20:15:11 -0500112 dw_pcie_writel_rc(pp, PCIE_ARUSER_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200113
Bjorn Helgaas76876952016-10-11 20:15:11 -0500114 reg = dw_pcie_readl_rc(pp, PCIE_AWUSER_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200115 reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
116 reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
Bjorn Helgaas76876952016-10-11 20:15:11 -0500117 dw_pcie_writel_rc(pp, PCIE_AWUSER_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200118
119 /* Enable INT A-D interrupts */
Bjorn Helgaas76876952016-10-11 20:15:11 -0500120 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_MASK1_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200121 reg |= PCIE_INT_A_ASSERT_MASK | PCIE_INT_B_ASSERT_MASK |
122 PCIE_INT_C_ASSERT_MASK | PCIE_INT_D_ASSERT_MASK;
Bjorn Helgaas76876952016-10-11 20:15:11 -0500123 dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_MASK1_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200124
125 if (!dw_pcie_link_up(pp)) {
126 /* Configuration done. Start LTSSM */
Bjorn Helgaas76876952016-10-11 20:15:11 -0500127 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200128 reg |= PCIE_APP_LTSSM_EN;
Bjorn Helgaas76876952016-10-11 20:15:11 -0500129 dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200130 }
131
132 /* Wait until the link becomes active again */
133 if (dw_pcie_wait_for_link(pp))
134 dev_err(pp->dev, "Link not up after reconfiguration\n");
135}
136
137static void armada8k_pcie_host_init(struct pcie_port *pp)
138{
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500139 struct armada8k_pcie *pcie = to_armada8k_pcie(pp);
140
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200141 dw_pcie_setup_rc(pp);
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500142 armada8k_pcie_establish_link(pcie);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200143}
144
145static irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg)
146{
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500147 struct armada8k_pcie *pcie = arg;
148 struct pcie_port *pp = &pcie->pp;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200149 u32 val;
150
151 /*
152 * Interrupts are directly handled by the device driver of the
153 * PCI device. However, they are also latched into the PCIe
154 * controller, so we simply discard them.
155 */
Bjorn Helgaas76876952016-10-11 20:15:11 -0500156 val = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG);
157 dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG, val);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200158
159 return IRQ_HANDLED;
160}
161
162static struct pcie_host_ops armada8k_pcie_host_ops = {
163 .link_up = armada8k_pcie_link_up,
164 .host_init = armada8k_pcie_host_init,
165};
166
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500167static int armada8k_add_pcie_port(struct armada8k_pcie *pcie,
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200168 struct platform_device *pdev)
169{
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500170 struct pcie_port *pp = &pcie->pp;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200171 struct device *dev = &pdev->dev;
172 int ret;
173
174 pp->root_bus_nr = -1;
175 pp->ops = &armada8k_pcie_host_ops;
176
177 pp->irq = platform_get_irq(pdev, 0);
178 if (!pp->irq) {
179 dev_err(dev, "failed to get irq for port\n");
180 return -ENODEV;
181 }
182
183 ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler,
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500184 IRQF_SHARED, "armada8k-pcie", pcie);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200185 if (ret) {
186 dev_err(dev, "failed to request irq %d\n", pp->irq);
187 return ret;
188 }
189
190 ret = dw_pcie_host_init(pp);
191 if (ret) {
192 dev_err(dev, "failed to initialize host: %d\n", ret);
193 return ret;
194 }
195
196 return 0;
197}
198
199static int armada8k_pcie_probe(struct platform_device *pdev)
200{
201 struct armada8k_pcie *pcie;
202 struct pcie_port *pp;
203 struct device *dev = &pdev->dev;
204 struct resource *base;
205 int ret;
206
207 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
208 if (!pcie)
209 return -ENOMEM;
210
211 pcie->clk = devm_clk_get(dev, NULL);
212 if (IS_ERR(pcie->clk))
213 return PTR_ERR(pcie->clk);
214
215 clk_prepare_enable(pcie->clk);
216
217 pp = &pcie->pp;
218 pp->dev = dev;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200219
220 /* Get the dw-pcie unit configuration/control registers base. */
221 base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl");
222 pp->dbi_base = devm_ioremap_resource(dev, base);
223 if (IS_ERR(pp->dbi_base)) {
224 dev_err(dev, "couldn't remap regs base %p\n", base);
225 ret = PTR_ERR(pp->dbi_base);
226 goto fail;
227 }
228
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500229 ret = armada8k_add_pcie_port(pcie, pdev);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200230 if (ret)
231 goto fail;
232
233 return 0;
234
235fail:
236 if (!IS_ERR(pcie->clk))
237 clk_disable_unprepare(pcie->clk);
238
239 return ret;
240}
241
242static const struct of_device_id armada8k_pcie_of_match[] = {
243 { .compatible = "marvell,armada8k-pcie", },
244 {},
245};
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200246
247static struct platform_driver armada8k_pcie_driver = {
248 .probe = armada8k_pcie_probe,
249 .driver = {
250 .name = "armada8k-pcie",
251 .of_match_table = of_match_ptr(armada8k_pcie_of_match),
252 },
253};
Paul Gortmaker0e6f98c2016-07-02 19:13:21 -0400254builtin_platform_driver(armada8k_pcie_driver);