blob: fbf001a068a46611a65d37fd6f63f9f61b44b19f [file] [log] [blame]
Ganesan Ramalingam9bac6242012-07-24 17:28:54 +02001/*
2 * Copyright (c) 2003-2012 Broadcom Corporation
3 * All Rights Reserved
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the Broadcom
9 * license below:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/kernel.h>
38#include <linux/init.h>
39#include <linux/msi.h>
40#include <linux/mm.h>
41#include <linux/irq.h>
42#include <linux/irqdesc.h>
43#include <linux/console.h>
44
45#include <asm/io.h>
46
47#include <asm/netlogic/interrupt.h>
48#include <asm/netlogic/haldefs.h>
49
50#include <asm/netlogic/xlp-hal/iomap.h>
51#include <asm/netlogic/xlp-hal/pic.h>
52#include <asm/netlogic/xlp-hal/xlp.h>
53#include <asm/netlogic/xlp-hal/pcibus.h>
54#include <asm/netlogic/xlp-hal/bridge.h>
55
56static void *pci_config_base;
57
58#define pci_cfg_addr(bus, devfn, off) (((bus) << 20) | ((devfn) << 12) | (off))
59
60/* PCI ops */
61static inline u32 pci_cfg_read_32bit(struct pci_bus *bus, unsigned int devfn,
62 int where)
63{
64 u32 data;
65 u32 *cfgaddr;
66
Jayachandran C8cd3d642013-01-14 15:11:58 +000067 where &= ~3;
68 if (bus->number == 0 && PCI_SLOT(devfn) == 1 && where == 0x954)
69 return 0xffffffff;
70
Ganesan Ramalingam9bac6242012-07-24 17:28:54 +020071 cfgaddr = (u32 *)(pci_config_base +
Jayachandran C8cd3d642013-01-14 15:11:58 +000072 pci_cfg_addr(bus->number, devfn, where));
Ganesan Ramalingam9bac6242012-07-24 17:28:54 +020073 data = *cfgaddr;
74 return data;
75}
76
77static inline void pci_cfg_write_32bit(struct pci_bus *bus, unsigned int devfn,
78 int where, u32 data)
79{
80 u32 *cfgaddr;
81
82 cfgaddr = (u32 *)(pci_config_base +
83 pci_cfg_addr(bus->number, devfn, where & ~3));
84 *cfgaddr = data;
85}
86
87static int nlm_pcibios_read(struct pci_bus *bus, unsigned int devfn,
88 int where, int size, u32 *val)
89{
90 u32 data;
91
92 if ((size == 2) && (where & 1))
93 return PCIBIOS_BAD_REGISTER_NUMBER;
94 else if ((size == 4) && (where & 3))
95 return PCIBIOS_BAD_REGISTER_NUMBER;
96
97 data = pci_cfg_read_32bit(bus, devfn, where);
98
99 if (size == 1)
100 *val = (data >> ((where & 3) << 3)) & 0xff;
101 else if (size == 2)
102 *val = (data >> ((where & 3) << 3)) & 0xffff;
103 else
104 *val = data;
105
106 return PCIBIOS_SUCCESSFUL;
107}
108
109
110static int nlm_pcibios_write(struct pci_bus *bus, unsigned int devfn,
111 int where, int size, u32 val)
112{
113 u32 data;
114
115 if ((size == 2) && (where & 1))
116 return PCIBIOS_BAD_REGISTER_NUMBER;
117 else if ((size == 4) && (where & 3))
118 return PCIBIOS_BAD_REGISTER_NUMBER;
119
120 data = pci_cfg_read_32bit(bus, devfn, where);
121
122 if (size == 1)
123 data = (data & ~(0xff << ((where & 3) << 3))) |
124 (val << ((where & 3) << 3));
125 else if (size == 2)
126 data = (data & ~(0xffff << ((where & 3) << 3))) |
127 (val << ((where & 3) << 3));
128 else
129 data = val;
130
131 pci_cfg_write_32bit(bus, devfn, where, data);
132
133 return PCIBIOS_SUCCESSFUL;
134}
135
136struct pci_ops nlm_pci_ops = {
137 .read = nlm_pcibios_read,
138 .write = nlm_pcibios_write
139};
140
141static struct resource nlm_pci_mem_resource = {
142 .name = "XLP PCI MEM",
143 .start = 0xd0000000UL, /* 256MB PCI mem @ 0xd000_0000 */
144 .end = 0xdfffffffUL,
145 .flags = IORESOURCE_MEM,
146};
147
148static struct resource nlm_pci_io_resource = {
149 .name = "XLP IO MEM",
150 .start = 0x14000000UL, /* 64MB PCI IO @ 0x1000_0000 */
151 .end = 0x17ffffffUL,
152 .flags = IORESOURCE_IO,
153};
154
155struct pci_controller nlm_pci_controller = {
156 .index = 0,
157 .pci_ops = &nlm_pci_ops,
158 .mem_resource = &nlm_pci_mem_resource,
159 .mem_offset = 0x00000000UL,
160 .io_resource = &nlm_pci_io_resource,
161 .io_offset = 0x00000000UL,
162};
163
164static int get_irq_vector(const struct pci_dev *dev)
165{
166 /*
167 * For XLP PCIe, there is an IRQ per Link, find out which
168 * link the device is on to assign interrupts
169 */
170 if (dev->bus->self == NULL)
171 return 0;
172
173 switch (dev->bus->self->devfn) {
174 case 0x8:
175 return PIC_PCIE_LINK_0_IRQ;
176 case 0x9:
177 return PIC_PCIE_LINK_1_IRQ;
178 case 0xa:
179 return PIC_PCIE_LINK_2_IRQ;
180 case 0xb:
181 return PIC_PCIE_LINK_3_IRQ;
182 }
183 WARN(1, "Unexpected devfn %d\n", dev->bus->self->devfn);
184 return 0;
185}
186
187int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
188{
189 return get_irq_vector(dev);
190}
191
192/* Do platform specific device initialization at pci_enable_device() time */
193int pcibios_plat_dev_init(struct pci_dev *dev)
194{
195 return 0;
196}
197
Jayachandran Ca264b5e2013-01-16 11:12:40 +0000198/*
199 * If big-endian, enable hardware byteswap on the PCIe bridges.
200 * This will make both the SoC and PCIe devices behave consistently with
201 * readl/writel.
202 */
203#ifdef __BIG_ENDIAN
204static void xlp_config_pci_bswap(void)
Ganesan Ramalingam9bac6242012-07-24 17:28:54 +0200205{
206 uint64_t pciebase, sysbase;
207 int node, i;
208 u32 reg;
209
210 /* Chip-0 so node set to 0 */
211 node = 0;
212 sysbase = nlm_get_bridge_regbase(node);
213 /*
214 * Enable byte swap in hardware. Program each link's PCIe SWAP regions
215 * from the link's address ranges.
216 */
217 for (i = 0; i < 4; i++) {
218 pciebase = nlm_pcicfg_base(XLP_IO_PCIE_OFFSET(node, i));
219 if (nlm_read_pci_reg(pciebase, 0) == 0xffffffff)
220 continue;
221
222 reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEMEM_BASE0 + i);
223 nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_MEM_BASE, reg);
224
225 reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEMEM_LIMIT0 + i);
226 nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_MEM_LIM,
227 reg | 0xfff);
228
229 reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEIO_BASE0 + i);
230 nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_IO_BASE, reg);
231
232 reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEIO_LIMIT0 + i);
233 nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_IO_LIM, reg | 0xfff);
234 }
Ganesan Ramalingam9bac6242012-07-24 17:28:54 +0200235}
Jayachandran Ca264b5e2013-01-16 11:12:40 +0000236#else
237/* Swap configuration not needed in little-endian mode */
238static inline void xlp_config_pci_bswap(void) {}
239#endif /* __BIG_ENDIAN */
Ganesan Ramalingam9bac6242012-07-24 17:28:54 +0200240
241static int __init pcibios_init(void)
242{
243 /* Firmware assigns PCI resources */
244 pci_set_flags(PCI_PROBE_ONLY);
245 pci_config_base = ioremap(XLP_DEFAULT_PCI_ECFG_BASE, 64 << 20);
246
247 /* Extend IO port for memory mapped io */
248 ioport_resource.start = 0;
249 ioport_resource.end = ~0;
250
Jayachandran Ca264b5e2013-01-16 11:12:40 +0000251 xlp_config_pci_bswap();
Ganesan Ramalingam9bac6242012-07-24 17:28:54 +0200252 set_io_port_base(CKSEG1);
253 nlm_pci_controller.io_map_base = CKSEG1;
254
255 register_pci_controller(&nlm_pci_controller);
256 pr_info("XLP PCIe Controller %pR%pR.\n", &nlm_pci_io_resource,
257 &nlm_pci_mem_resource);
258
259 return 0;
260}
261arch_initcall(pcibios_init);