powercap: restrict energy meter to root access
[linux/fpc-iii.git] / drivers / pci / host / pcie-armada8k.c
blob0ac0f18690f20a6743193cb9714feaf72f309487
1 /*
2 * PCIe host controller driver for Marvell Armada-8K SoCs
4 * Armada-8K PCIe Glue Layer Source Code
6 * Copyright (C) 2016 Marvell Technology Group Ltd.
8 * Author: Yehuda Yitshak <yehuday@marvell.com>
9 * Author: Shadi Ammouri <shadi@marvell.com>
11 * 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.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #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>
29 #include "pcie-designware.h"
31 struct armada8k_pcie {
32 struct pcie_port pp; /* pp.dbi_base is DT ctrl */
33 struct clk *clk;
36 #define PCIE_VENDOR_REGS_OFFSET 0x8000
38 #define PCIE_GLOBAL_CONTROL_REG (PCIE_VENDOR_REGS_OFFSET + 0x0)
39 #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 */
44 #define PCIE_GLOBAL_STATUS_REG (PCIE_VENDOR_REGS_OFFSET + 0x8)
45 #define PCIE_GLB_STS_RDLH_LINK_UP BIT(1)
46 #define PCIE_GLB_STS_PHY_LINK_UP BIT(9)
48 #define PCIE_GLOBAL_INT_CAUSE1_REG (PCIE_VENDOR_REGS_OFFSET + 0x1C)
49 #define PCIE_GLOBAL_INT_MASK1_REG (PCIE_VENDOR_REGS_OFFSET + 0x20)
50 #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)
55 #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)
60 * AR/AW Cache defauls: Normal memory, Write-Back, Read / Write
61 * allocate
63 #define ARCACHE_DEFAULT_VALUE 0x3511
64 #define AWCACHE_DEFAULT_VALUE 0x5311
66 #define DOMAIN_OUTER_SHAREABLE 0x2
67 #define AX_USER_DOMAIN_MASK 0x3
68 #define AX_USER_DOMAIN_SHIFT 4
70 #define to_armada8k_pcie(x) container_of(x, struct armada8k_pcie, pp)
72 static int armada8k_pcie_link_up(struct pcie_port *pp)
74 u32 reg;
75 u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
77 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_STATUS_REG);
79 if ((reg & mask) == mask)
80 return 1;
82 dev_dbg(pp->dev, "No link detected (Global-Status: 0x%08x).\n", reg);
83 return 0;
86 static void armada8k_pcie_establish_link(struct armada8k_pcie *pcie)
88 struct pcie_port *pp = &pcie->pp;
89 u32 reg;
91 if (!dw_pcie_link_up(pp)) {
92 /* Disable LTSSM state machine to enable configuration */
93 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
94 reg &= ~(PCIE_APP_LTSSM_EN);
95 dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
98 /* Set the device to root complex mode */
99 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
100 reg &= ~(PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_SHIFT);
101 reg |= PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_SHIFT;
102 dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
104 /* Set the PCIe master AxCache attributes */
105 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);
108 /* Set the PCIe master AxDomain attributes */
109 reg = dw_pcie_readl_rc(pp, PCIE_ARUSER_REG);
110 reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
111 reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
112 dw_pcie_writel_rc(pp, PCIE_ARUSER_REG, reg);
114 reg = dw_pcie_readl_rc(pp, PCIE_AWUSER_REG);
115 reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
116 reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
117 dw_pcie_writel_rc(pp, PCIE_AWUSER_REG, reg);
119 /* Enable INT A-D interrupts */
120 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_MASK1_REG);
121 reg |= PCIE_INT_A_ASSERT_MASK | PCIE_INT_B_ASSERT_MASK |
122 PCIE_INT_C_ASSERT_MASK | PCIE_INT_D_ASSERT_MASK;
123 dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_MASK1_REG, reg);
125 if (!dw_pcie_link_up(pp)) {
126 /* Configuration done. Start LTSSM */
127 reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
128 reg |= PCIE_APP_LTSSM_EN;
129 dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
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");
137 static void armada8k_pcie_host_init(struct pcie_port *pp)
139 struct armada8k_pcie *pcie = to_armada8k_pcie(pp);
141 dw_pcie_setup_rc(pp);
142 armada8k_pcie_establish_link(pcie);
145 static irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg)
147 struct armada8k_pcie *pcie = arg;
148 struct pcie_port *pp = &pcie->pp;
149 u32 val;
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.
156 val = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG);
157 dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG, val);
159 return IRQ_HANDLED;
162 static struct pcie_host_ops armada8k_pcie_host_ops = {
163 .link_up = armada8k_pcie_link_up,
164 .host_init = armada8k_pcie_host_init,
167 static int armada8k_add_pcie_port(struct armada8k_pcie *pcie,
168 struct platform_device *pdev)
170 struct pcie_port *pp = &pcie->pp;
171 struct device *dev = &pdev->dev;
172 int ret;
174 pp->root_bus_nr = -1;
175 pp->ops = &armada8k_pcie_host_ops;
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;
183 ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler,
184 IRQF_SHARED, "armada8k-pcie", pcie);
185 if (ret) {
186 dev_err(dev, "failed to request irq %d\n", pp->irq);
187 return ret;
190 ret = dw_pcie_host_init(pp);
191 if (ret) {
192 dev_err(dev, "failed to initialize host: %d\n", ret);
193 return ret;
196 return 0;
199 static int armada8k_pcie_probe(struct platform_device *pdev)
201 struct armada8k_pcie *pcie;
202 struct pcie_port *pp;
203 struct device *dev = &pdev->dev;
204 struct resource *base;
205 int ret;
207 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
208 if (!pcie)
209 return -ENOMEM;
211 pcie->clk = devm_clk_get(dev, NULL);
212 if (IS_ERR(pcie->clk))
213 return PTR_ERR(pcie->clk);
215 clk_prepare_enable(pcie->clk);
217 pp = &pcie->pp;
218 pp->dev = dev;
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;
229 ret = armada8k_add_pcie_port(pcie, pdev);
230 if (ret)
231 goto fail;
233 return 0;
235 fail:
236 if (!IS_ERR(pcie->clk))
237 clk_disable_unprepare(pcie->clk);
239 return ret;
242 static const struct of_device_id armada8k_pcie_of_match[] = {
243 { .compatible = "marvell,armada8k-pcie", },
247 static 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),
254 builtin_platform_driver(armada8k_pcie_driver);