blob: 3ff31301323b0d877739c1048f9d8d3d0735d021 [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 {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053032 struct dw_pcie *pci;
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
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053070#define to_armada8k_pcie(x) dev_get_drvdata((x)->dev)
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020071
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053072static int armada8k_pcie_link_up(struct dw_pcie *pci)
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020073{
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
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053077 reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_STATUS_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020078
79 if ((reg & mask) == mask)
80 return 1;
81
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053082 dev_dbg(pci->dev, "No link detected (Global-Status: 0x%08x).\n", reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020083 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{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053088 struct dw_pcie *pci = pcie->pci;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020089 u32 reg;
90
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053091 if (!dw_pcie_link_up(pci)) {
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020092 /* Disable LTSSM state machine to enable configuration */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053093 reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020094 reg &= ~(PCIE_APP_LTSSM_EN);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053095 dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +020096 }
97
98 /* Set the device to root complex mode */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053099 reg = dw_pcie_readl_dbi(pci, 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;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530102 dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200103
104 /* Set the PCIe master AxCache attributes */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530105 dw_pcie_writel_dbi(pci, PCIE_ARCACHE_TRC_REG, ARCACHE_DEFAULT_VALUE);
106 dw_pcie_writel_dbi(pci, PCIE_AWCACHE_TRC_REG, AWCACHE_DEFAULT_VALUE);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200107
108 /* Set the PCIe master AxDomain attributes */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530109 reg = dw_pcie_readl_dbi(pci, 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;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530112 dw_pcie_writel_dbi(pci, PCIE_ARUSER_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200113
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530114 reg = dw_pcie_readl_dbi(pci, 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;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530117 dw_pcie_writel_dbi(pci, PCIE_AWUSER_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200118
119 /* Enable INT A-D interrupts */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530120 reg = dw_pcie_readl_dbi(pci, 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;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530123 dw_pcie_writel_dbi(pci, PCIE_GLOBAL_INT_MASK1_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200124
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530125 if (!dw_pcie_link_up(pci)) {
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200126 /* Configuration done. Start LTSSM */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530127 reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200128 reg |= PCIE_APP_LTSSM_EN;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530129 dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200130 }
131
132 /* Wait until the link becomes active again */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530133 if (dw_pcie_wait_for_link(pci))
134 dev_err(pci->dev, "Link not up after reconfiguration\n");
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200135}
136
137static void armada8k_pcie_host_init(struct pcie_port *pp)
138{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530139 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
140 struct armada8k_pcie *pcie = to_armada8k_pcie(pci);
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500141
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200142 dw_pcie_setup_rc(pp);
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500143 armada8k_pcie_establish_link(pcie);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200144}
145
146static irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg)
147{
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500148 struct armada8k_pcie *pcie = arg;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530149 struct dw_pcie *pci = pcie->pci;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200150 u32 val;
151
152 /*
153 * Interrupts are directly handled by the device driver of the
154 * PCI device. However, they are also latched into the PCIe
155 * controller, so we simply discard them.
156 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530157 val = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_INT_CAUSE1_REG);
158 dw_pcie_writel_dbi(pci, PCIE_GLOBAL_INT_CAUSE1_REG, val);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200159
160 return IRQ_HANDLED;
161}
162
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530163static struct dw_pcie_host_ops armada8k_pcie_host_ops = {
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200164 .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{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530170 struct dw_pcie *pci = pcie->pci;
171 struct pcie_port *pp = &pci->pp;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200172 struct device *dev = &pdev->dev;
173 int ret;
174
175 pp->root_bus_nr = -1;
176 pp->ops = &armada8k_pcie_host_ops;
177
178 pp->irq = platform_get_irq(pdev, 0);
179 if (!pp->irq) {
180 dev_err(dev, "failed to get irq for port\n");
181 return -ENODEV;
182 }
183
184 ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler,
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500185 IRQF_SHARED, "armada8k-pcie", pcie);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200186 if (ret) {
187 dev_err(dev, "failed to request irq %d\n", pp->irq);
188 return ret;
189 }
190
191 ret = dw_pcie_host_init(pp);
192 if (ret) {
193 dev_err(dev, "failed to initialize host: %d\n", ret);
194 return ret;
195 }
196
197 return 0;
198}
199
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530200static const struct dw_pcie_ops dw_pcie_ops = {
201 .link_up = armada8k_pcie_link_up,
202};
203
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200204static int armada8k_pcie_probe(struct platform_device *pdev)
205{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530206 struct dw_pcie *pci;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200207 struct armada8k_pcie *pcie;
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200208 struct device *dev = &pdev->dev;
209 struct resource *base;
210 int ret;
211
212 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
213 if (!pcie)
214 return -ENOMEM;
215
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530216 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
217 if (!pci)
218 return -ENOMEM;
219
220 pci->dev = dev;
221 pci->ops = &dw_pcie_ops;
222
Guenter Roeckc0464062017-02-25 02:08:12 -0800223 pcie->pci = pci;
224
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200225 pcie->clk = devm_clk_get(dev, NULL);
226 if (IS_ERR(pcie->clk))
227 return PTR_ERR(pcie->clk);
228
229 clk_prepare_enable(pcie->clk);
230
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200231 /* Get the dw-pcie unit configuration/control registers base. */
232 base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl");
Lorenzo Pieralisi53dfa172017-04-19 17:49:04 +0100233 pci->dbi_base = devm_pci_remap_cfg_resource(dev, base);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530234 if (IS_ERR(pci->dbi_base)) {
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200235 dev_err(dev, "couldn't remap regs base %p\n", base);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530236 ret = PTR_ERR(pci->dbi_base);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200237 goto fail;
238 }
239
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530240 platform_set_drvdata(pdev, pcie);
241
Bjorn Helgaasb2d6fd72016-10-06 13:30:00 -0500242 ret = armada8k_add_pcie_port(pcie, pdev);
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200243 if (ret)
244 goto fail;
245
246 return 0;
247
248fail:
249 if (!IS_ERR(pcie->clk))
250 clk_disable_unprepare(pcie->clk);
251
252 return ret;
253}
254
255static const struct of_device_id armada8k_pcie_of_match[] = {
256 { .compatible = "marvell,armada8k-pcie", },
257 {},
258};
Thomas Petazzoni1c52a512016-04-26 10:31:46 +0200259
260static struct platform_driver armada8k_pcie_driver = {
261 .probe = armada8k_pcie_probe,
262 .driver = {
263 .name = "armada8k-pcie",
264 .of_match_table = of_match_ptr(armada8k_pcie_of_match),
265 },
266};
Paul Gortmaker0e6f98c2016-07-02 19:13:21 -0400267builtin_platform_driver(armada8k_pcie_driver);