blob: 318394ed5c7a97c2923c8c35134b88cb188ef238 [file] [log] [blame]
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +04001/*
2 * PCI-E support for CNS3xxx
3 *
4 * Copyright 2008 Cavium Networks
5 * Richard Liu <richard.liu@caviumnetworks.com>
6 * Copyright 2010 MontaVista Software, LLC.
7 * Anton Vorontsov <avorontsov@mvista.com>
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/bug.h>
17#include <linux/pci.h>
18#include <linux/io.h>
19#include <linux/ioport.h>
20#include <linux/interrupt.h>
21#include <linux/ptrace.h>
22#include <asm/mach/map.h>
Arnd Bergmann3f9fb2a2013-03-13 13:15:25 +010023#include "cns3xxx.h"
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040024#include "core.h"
25
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040026struct cns3xxx_pcie {
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +010027 void __iomem *host_regs; /* PCI config registers for host bridge */
28 void __iomem *cfg0_regs; /* PCI Type 0 config registers */
29 void __iomem *cfg1_regs; /* PCI Type 1 config registers */
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040030 unsigned int irqs[2];
31 struct resource res_io;
32 struct resource res_mem;
Lorenzo Pieralisic88d54b2014-11-21 11:29:25 +000033 int port;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040034 bool linked;
35};
36
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040037static struct cns3xxx_pcie *sysdata_to_cnspci(void *sysdata)
38{
39 struct pci_sys_data *root = sysdata;
40
Lorenzo Pieralisic88d54b2014-11-21 11:29:25 +000041 return root->private_data;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040042}
43
Anton Vorontsov7caaf7e2011-09-09 23:18:00 +040044static struct cns3xxx_pcie *pdev_to_cnspci(const struct pci_dev *dev)
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040045{
46 return sysdata_to_cnspci(dev->sysdata);
47}
48
49static struct cns3xxx_pcie *pbus_to_cnspci(struct pci_bus *bus)
50{
51 return sysdata_to_cnspci(bus->sysdata);
52}
53
Rob Herring802b7c02015-01-09 20:34:40 -060054static void __iomem *cns3xxx_pci_map_bus(struct pci_bus *bus,
55 unsigned int devfn, int where)
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040056{
57 struct cns3xxx_pcie *cnspci = pbus_to_cnspci(bus);
58 int busno = bus->number;
59 int slot = PCI_SLOT(devfn);
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040060 void __iomem *base;
61
62 /* If there is no link, just show the CNS PCI bridge. */
Krzysztof Hałasadefaa4d2014-09-16 12:36:32 +020063 if (!cnspci->linked && busno > 0)
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040064 return NULL;
65
66 /*
67 * The CNS PCI bridge doesn't fit into the PCI hierarchy, though
Arnd Bergmann498a92d2015-10-07 22:05:49 +020068 * we still want to access it.
69 * We place the host bridge on bus 0, and the directly connected
70 * device on bus 1, slot 0.
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040071 */
Krzysztof Hałasadefaa4d2014-09-16 12:36:32 +020072 if (busno == 0) { /* internal PCIe bus, host bridge device */
73 if (devfn == 0) /* device# and function# are ignored by hw */
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +010074 base = cnspci->host_regs;
Krzysztof Hałasadefaa4d2014-09-16 12:36:32 +020075 else
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +010076 return NULL; /* no such device */
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040077
Krzysztof Hałasadefaa4d2014-09-16 12:36:32 +020078 } else if (busno == 1) { /* directly connected PCIe device */
79 if (slot == 0) /* device# is ignored by hw */
80 base = cnspci->cfg0_regs;
81 else
82 return NULL; /* no such device */
83 } else /* remote PCI bus */
84 base = cnspci->cfg1_regs + ((busno & 0xf) << 20);
85
86 return base + (where & 0xffc) + (devfn << 12);
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040087}
88
89static int cns3xxx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
90 int where, int size, u32 *val)
91{
Rob Herring802b7c02015-01-09 20:34:40 -060092 int ret;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040093 u32 mask = (0x1ull << (size * 8)) - 1;
94 int shift = (where % 4) * 8;
95
Rob Herring802b7c02015-01-09 20:34:40 -060096 ret = pci_generic_config_read32(bus, devfn, where, size, val);
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +040097
Rob Herring802b7c02015-01-09 20:34:40 -060098 if (ret == PCIBIOS_SUCCESSFUL && !bus->number && !devfn &&
99 (where & 0xffc) == PCI_CLASS_REVISION)
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400100 /*
101 * RC's class is 0xb, but Linux PCI driver needs 0x604
102 * for a PCIe bridge. So we must fixup the class code
103 * to 0x604 here.
104 */
Rob Herring802b7c02015-01-09 20:34:40 -0600105 *val = ((((*val << shift) & 0xff) | (0x604 << 16)) >> shift) & mask;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400106
Rob Herring802b7c02015-01-09 20:34:40 -0600107 return ret;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400108}
109
110static int cns3xxx_pci_setup(int nr, struct pci_sys_data *sys)
111{
112 struct cns3xxx_pcie *cnspci = sysdata_to_cnspci(sys);
113 struct resource *res_io = &cnspci->res_io;
114 struct resource *res_mem = &cnspci->res_mem;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400115
116 BUG_ON(request_resource(&iomem_resource, res_io) ||
117 request_resource(&iomem_resource, res_mem));
118
Bjorn Helgaas9f786d02012-02-23 20:19:01 -0700119 pci_add_resource_offset(&sys->resources, res_io, sys->io_offset);
120 pci_add_resource_offset(&sys->resources, res_mem, sys->mem_offset);
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400121
122 return 1;
123}
124
125static struct pci_ops cns3xxx_pcie_ops = {
Rob Herring802b7c02015-01-09 20:34:40 -0600126 .map_bus = cns3xxx_pci_map_bus,
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400127 .read = cns3xxx_pci_read_config,
Rob Herring802b7c02015-01-09 20:34:40 -0600128 .write = pci_generic_config_write,
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400129};
130
Ralf Baechled5341942011-06-10 15:30:21 +0100131static int cns3xxx_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400132{
133 struct cns3xxx_pcie *cnspci = pdev_to_cnspci(dev);
Krzysztof Hałasadefaa4d2014-09-16 12:36:32 +0200134 int irq = cnspci->irqs[!!dev->bus->number];
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400135
136 pr_info("PCIe map irq: %04d:%02x:%02x.%02x slot %d, pin %d, irq: %d\n",
137 pci_domain_nr(dev->bus), dev->bus->number, PCI_SLOT(dev->devfn),
138 PCI_FUNC(dev->devfn), slot, pin, irq);
139
140 return irq;
141}
142
143static struct cns3xxx_pcie cns3xxx_pcie[] = {
144 [0] = {
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +0100145 .host_regs = (void __iomem *)CNS3XXX_PCIE0_HOST_BASE_VIRT,
146 .cfg0_regs = (void __iomem *)CNS3XXX_PCIE0_CFG0_BASE_VIRT,
147 .cfg1_regs = (void __iomem *)CNS3XXX_PCIE0_CFG1_BASE_VIRT,
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400148 .res_io = {
149 .name = "PCIe0 I/O space",
150 .start = CNS3XXX_PCIE0_IO_BASE,
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +0100151 .end = CNS3XXX_PCIE0_CFG0_BASE - 1, /* 16 MiB */
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400152 .flags = IORESOURCE_IO,
153 },
154 .res_mem = {
155 .name = "PCIe0 non-prefetchable",
156 .start = CNS3XXX_PCIE0_MEM_BASE,
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +0100157 .end = CNS3XXX_PCIE0_HOST_BASE - 1, /* 176 MiB */
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400158 .flags = IORESOURCE_MEM,
159 },
160 .irqs = { IRQ_CNS3XXX_PCIE0_RC, IRQ_CNS3XXX_PCIE0_DEVICE, },
Lorenzo Pieralisic88d54b2014-11-21 11:29:25 +0000161 .port = 0,
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400162 },
163 [1] = {
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +0100164 .host_regs = (void __iomem *)CNS3XXX_PCIE1_HOST_BASE_VIRT,
165 .cfg0_regs = (void __iomem *)CNS3XXX_PCIE1_CFG0_BASE_VIRT,
166 .cfg1_regs = (void __iomem *)CNS3XXX_PCIE1_CFG1_BASE_VIRT,
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400167 .res_io = {
168 .name = "PCIe1 I/O space",
169 .start = CNS3XXX_PCIE1_IO_BASE,
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +0100170 .end = CNS3XXX_PCIE1_CFG0_BASE - 1, /* 16 MiB */
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400171 .flags = IORESOURCE_IO,
172 },
173 .res_mem = {
174 .name = "PCIe1 non-prefetchable",
175 .start = CNS3XXX_PCIE1_MEM_BASE,
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +0100176 .end = CNS3XXX_PCIE1_HOST_BASE - 1, /* 176 MiB */
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400177 .flags = IORESOURCE_MEM,
178 },
179 .irqs = { IRQ_CNS3XXX_PCIE1_RC, IRQ_CNS3XXX_PCIE1_DEVICE, },
Lorenzo Pieralisic88d54b2014-11-21 11:29:25 +0000180 .port = 1,
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400181 },
182};
183
184static void __init cns3xxx_pcie_check_link(struct cns3xxx_pcie *cnspci)
185{
Lorenzo Pieralisic88d54b2014-11-21 11:29:25 +0000186 int port = cnspci->port;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400187 u32 reg;
188 unsigned long time;
189
190 reg = __raw_readl(MISC_PCIE_CTRL(port));
191 /*
192 * Enable Application Request to 1, it will exit L1 automatically,
193 * but when chip back, it will use another clock, still can use 0x1.
194 */
195 reg |= 0x3;
196 __raw_writel(reg, MISC_PCIE_CTRL(port));
197
198 pr_info("PCIe: Port[%d] Enable PCIe LTSSM\n", port);
199 pr_info("PCIe: Port[%d] Check data link layer...", port);
200
201 time = jiffies;
202 while (1) {
203 reg = __raw_readl(MISC_PCIE_PM_DEBUG(port));
204 if (reg & 0x1) {
205 pr_info("Link up.\n");
206 cnspci->linked = 1;
207 break;
208 } else if (time_after(jiffies, time + 50)) {
209 pr_info("Device not found.\n");
210 break;
211 }
212 }
213}
214
Arnd Bergmann498a92d2015-10-07 22:05:49 +0200215static void cns3xxx_write_config(struct cns3xxx_pcie *cnspci,
216 int where, int size, u32 val)
217{
218 void __iomem *base = cnspci->host_regs + (where & 0xffc);
219 u32 v;
220 u32 mask = (0x1ull << (size * 8)) - 1;
221 int shift = (where % 4) * 8;
222
Krzysztof Halasa88e9da92016-03-11 12:32:14 +0100223 v = readl_relaxed(base);
Arnd Bergmann498a92d2015-10-07 22:05:49 +0200224
225 v &= ~(mask << shift);
226 v |= (val & mask) << shift;
227
Krzysztof Halasa88e9da92016-03-11 12:32:14 +0100228 writel_relaxed(v, base);
229 readl_relaxed(base);
Arnd Bergmann498a92d2015-10-07 22:05:49 +0200230}
231
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400232static void __init cns3xxx_pcie_hw_init(struct cns3xxx_pcie *cnspci)
233{
Krzysztof Hałasa64cf9d02014-03-04 08:37:10 +0100234 u16 mem_base = cnspci->res_mem.start >> 16;
235 u16 mem_limit = cnspci->res_mem.end >> 16;
236 u16 io_base = cnspci->res_io.start >> 16;
237 u16 io_limit = cnspci->res_io.end >> 16;
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400238
Arnd Bergmann498a92d2015-10-07 22:05:49 +0200239 cns3xxx_write_config(cnspci, PCI_PRIMARY_BUS, 1, 0);
240 cns3xxx_write_config(cnspci, PCI_SECONDARY_BUS, 1, 1);
241 cns3xxx_write_config(cnspci, PCI_SUBORDINATE_BUS, 1, 1);
242 cns3xxx_write_config(cnspci, PCI_MEMORY_BASE, 2, mem_base);
243 cns3xxx_write_config(cnspci, PCI_MEMORY_LIMIT, 2, mem_limit);
244 cns3xxx_write_config(cnspci, PCI_IO_BASE_UPPER16, 2, io_base);
245 cns3xxx_write_config(cnspci, PCI_IO_LIMIT_UPPER16, 2, io_limit);
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400246
247 if (!cnspci->linked)
248 return;
249
250 /* Set Device Max_Read_Request_Size to 128 byte */
Arnd Bergmann498a92d2015-10-07 22:05:49 +0200251 pcie_bus_config = PCIE_BUS_PEER2PEER;
252
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400253 /* Disable PCIe0 Interrupt Mask INTA to INTD */
Arnd Bergmann498a92d2015-10-07 22:05:49 +0200254 __raw_writel(~0x3FFF, MISC_PCIE_INT_MASK(cnspci->port));
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400255}
256
257static int cns3xxx_pcie_abort_handler(unsigned long addr, unsigned int fsr,
258 struct pt_regs *regs)
259{
260 if (fsr & (1 << 10))
261 regs->ARM_pc += 4;
262 return 0;
263}
264
Xia Kaixu0a2e9122014-09-03 21:18:12 +0800265void __init cns3xxx_pcie_init_late(void)
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400266{
267 int i;
Lorenzo Pieralisic88d54b2014-11-21 11:29:25 +0000268 void *private_data;
269 struct hw_pci hw_pci = {
270 .nr_controllers = 1,
271 .ops = &cns3xxx_pcie_ops,
272 .setup = cns3xxx_pci_setup,
273 .map_irq = cns3xxx_pcie_map_irq,
274 .private_data = &private_data,
275 };
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400276
Rob Herringc9d95fb2011-06-28 21:16:13 -0500277 pcibios_min_io = 0;
278 pcibios_min_mem = 0;
279
Anton Vorontsov44266412010-11-29 18:46:22 +0300280 hook_fault_code(16 + 6, cns3xxx_pcie_abort_handler, SIGBUS, 0,
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400281 "imprecise external abort");
282
283 for (i = 0; i < ARRAY_SIZE(cns3xxx_pcie); i++) {
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400284 cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_PCIE(i));
285 cns3xxx_pwr_soft_rst(0x1 << PM_SOFT_RST_REG_OFFST_PCIE(i));
286 cns3xxx_pcie_check_link(&cns3xxx_pcie[i]);
287 cns3xxx_pcie_hw_init(&cns3xxx_pcie[i]);
Lorenzo Pieralisic88d54b2014-11-21 11:29:25 +0000288 private_data = &cns3xxx_pcie[i];
289 pci_common_init(&hw_pci);
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400290 }
291
292 pci_assign_unassigned_resources();
Anton Vorontsov5f32f7a2010-05-28 13:10:52 +0400293}