blob: f97e169393bc3060eb778835cbb1b4813f31b140 [file] [log] [blame]
Andrew Isaacsondc41f942005-10-19 23:58:49 -07001/*
2 * Copyright (C) 2001,2002,2005 Broadcom Corporation
3 * Copyright (C) 2004 by Ralf Baechle (ralf@linux-mips.org)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20/*
21 * BCM1x80/1x55-specific PCI support
22 *
23 * This module provides the glue between Linux's PCI subsystem
24 * and the hardware. We basically provide glue for accessing
25 * configuration space, and set up the translation for I/O
26 * space accesses.
27 *
28 * To access configuration space, we use ioremap. In the 32-bit
29 * kernel, this consumes either 4 or 8 page table pages, and 16MB of
30 * kernel mapped memory. Hopefully neither of these should be a huge
31 * problem.
32 *
33 * XXX: AT THIS TIME, ONLY the NATIVE PCI-X INTERFACE IS SUPPORTED.
34 */
Andrew Isaacsondc41f942005-10-19 23:58:49 -070035#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/kernel.h>
38#include <linux/init.h>
39#include <linux/mm.h>
40#include <linux/console.h>
41#include <linux/tty.h>
Markos Chandras88f02512013-09-19 10:27:52 +010042#include <linux/vt.h>
Andrew Isaacsondc41f942005-10-19 23:58:49 -070043
44#include <asm/sibyte/bcm1480_regs.h>
45#include <asm/sibyte/bcm1480_scd.h>
46#include <asm/sibyte/board.h>
47#include <asm/io.h>
48
49/*
50 * Macros for calculating offsets into config space given a device
51 * structure or dev/fun/reg
52 */
Ralf Baechle21a151d2007-10-11 23:46:15 +010053#define CFGOFFSET(bus, devfn, where) (((bus)<<16)+((devfn)<<8)+(where))
54#define CFGADDR(bus, devfn, where) CFGOFFSET((bus)->number, (devfn), where)
Andrew Isaacsondc41f942005-10-19 23:58:49 -070055
56static void *cfg_space;
57
Ralf Baechle70342282013-01-22 12:59:30 +010058#define PCI_BUS_ENABLED 1
59#define PCI_DEVICE_MODE 2
Andrew Isaacsondc41f942005-10-19 23:58:49 -070060
Ralf Baechle982f6ff2009-09-17 02:25:07 +020061static int bcm1480_bus_status;
Andrew Isaacsondc41f942005-10-19 23:58:49 -070062
63#define PCI_BRIDGE_DEVICE 0
Andrew Isaacsondc41f942005-10-19 23:58:49 -070064
65/*
66 * Read/write 32-bit values in config space.
67 */
68static inline u32 READCFG32(u32 addr)
69{
70 return *(u32 *)(cfg_space + (addr&~3));
71}
72
73static inline void WRITECFG32(u32 addr, u32 data)
74{
75 *(u32 *)(cfg_space + (addr & ~3)) = data;
76}
77
Ralf Baechle19df0d12007-07-10 17:33:00 +010078int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Andrew Isaacsondc41f942005-10-19 23:58:49 -070079{
Ralf Baechlef435a912007-12-06 17:15:57 +000080 if (pin == 0)
81 return -1;
82
83 return K_BCM1480_INT_PCI_INTA - 1 + pin;
Andrew Isaacsondc41f942005-10-19 23:58:49 -070084}
85
86/* Do platform specific device initialization at pci_enable_device() time */
87int pcibios_plat_dev_init(struct pci_dev *dev)
88{
89 return 0;
90}
91
92/*
93 * Some checks before doing config cycles:
94 * In PCI Device Mode, hide everything on bus 0 except the LDT host
95 * bridge. Otherwise, access is controlled by bridge MasterEn bits.
96 */
97static int bcm1480_pci_can_access(struct pci_bus *bus, int devfn)
98{
Andrew Isaacson8a1417d2005-10-19 23:59:11 -070099 u32 devno;
100
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700101 if (!(bcm1480_bus_status & (PCI_BUS_ENABLED | PCI_DEVICE_MODE)))
102 return 0;
103
104 if (bus->number == 0) {
Andrew Isaacson8a1417d2005-10-19 23:59:11 -0700105 devno = PCI_SLOT(devfn);
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000106 if (bcm1480_bus_status & PCI_DEVICE_MODE)
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700107 return 0;
108 else
109 return 1;
110 } else
111 return 1;
112}
113
114/*
115 * Read/write access functions for various sizes of values
116 * in config space. Return all 1's for disallowed accesses
117 * for a kludgy but adequate simulation of master aborts.
118 */
119
120static int bcm1480_pcibios_read(struct pci_bus *bus, unsigned int devfn,
121 int where, int size, u32 * val)
122{
123 u32 data = 0;
124
125 if ((size == 2) && (where & 1))
126 return PCIBIOS_BAD_REGISTER_NUMBER;
127 else if ((size == 4) && (where & 3))
128 return PCIBIOS_BAD_REGISTER_NUMBER;
129
130 if (bcm1480_pci_can_access(bus, devfn))
131 data = READCFG32(CFGADDR(bus, devfn, where));
132 else
133 data = 0xFFFFFFFF;
134
135 if (size == 1)
136 *val = (data >> ((where & 3) << 3)) & 0xff;
137 else if (size == 2)
138 *val = (data >> ((where & 3) << 3)) & 0xffff;
139 else
140 *val = data;
141
142 return PCIBIOS_SUCCESSFUL;
143}
144
145static int bcm1480_pcibios_write(struct pci_bus *bus, unsigned int devfn,
146 int where, int size, u32 val)
147{
148 u32 cfgaddr = CFGADDR(bus, devfn, where);
149 u32 data = 0;
150
151 if ((size == 2) && (where & 1))
152 return PCIBIOS_BAD_REGISTER_NUMBER;
153 else if ((size == 4) && (where & 3))
154 return PCIBIOS_BAD_REGISTER_NUMBER;
155
156 if (!bcm1480_pci_can_access(bus, devfn))
157 return PCIBIOS_BAD_REGISTER_NUMBER;
158
159 data = READCFG32(cfgaddr);
160
161 if (size == 1)
162 data = (data & ~(0xff << ((where & 3) << 3))) |
163 (val << ((where & 3) << 3));
164 else if (size == 2)
165 data = (data & ~(0xffff << ((where & 3) << 3))) |
166 (val << ((where & 3) << 3));
167 else
168 data = val;
169
170 WRITECFG32(cfgaddr, data);
171
172 return PCIBIOS_SUCCESSFUL;
173}
174
175struct pci_ops bcm1480_pci_ops = {
Rob Herring7b097772015-01-09 20:34:36 -0600176 .read = bcm1480_pcibios_read,
177 .write = bcm1480_pcibios_write,
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700178};
179
180static struct resource bcm1480_mem_resource = {
181 .name = "BCM1480 PCI MEM",
Ralf Baechle07c019b2008-01-29 10:14:59 +0000182 .start = A_BCM1480_PHYS_PCI_MEM_MATCH_BYTES,
183 .end = A_BCM1480_PHYS_PCI_MEM_MATCH_BYTES + 0xfffffffUL,
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700184 .flags = IORESOURCE_MEM,
185};
186
187static struct resource bcm1480_io_resource = {
188 .name = "BCM1480 PCI I/O",
Thomas Bogendoerfercf7b7e02008-03-16 18:14:16 +0100189 .start = A_BCM1480_PHYS_PCI_IO_MATCH_BYTES,
190 .end = A_BCM1480_PHYS_PCI_IO_MATCH_BYTES + 0x1ffffffUL,
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700191 .flags = IORESOURCE_IO,
192};
193
194struct pci_controller bcm1480_controller = {
195 .pci_ops = &bcm1480_pci_ops,
196 .mem_resource = &bcm1480_mem_resource,
197 .io_resource = &bcm1480_io_resource,
Ralf Baechle70342282013-01-22 12:59:30 +0100198 .io_offset = A_BCM1480_PHYS_PCI_IO_MATCH_BYTES,
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700199};
200
201
202static int __init bcm1480_pcibios_init(void)
203{
204 uint32_t cmdreg;
205 uint64_t reg;
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700206
207 /* CFE will assign PCI resources */
Bjorn Helgaas29090602012-02-23 20:18:57 -0700208 pci_set_flags(PCI_PROBE_ONLY);
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700209
210 /* Avoid ISA compat ranges. */
211 PCIBIOS_MIN_IO = 0x00008000UL;
212 PCIBIOS_MIN_MEM = 0x01000000UL;
213
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300214 /* Set I/O resource limits. - unlimited for now to accommodate HT */
Andrew Isaacson8a1417d2005-10-19 23:59:11 -0700215 ioport_resource.end = 0xffffffffUL;
216 iomem_resource.end = 0xffffffffUL;
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700217
218 cfg_space = ioremap(A_BCM1480_PHYS_PCI_CFG_MATCH_BITS, 16*1024*1024);
219
220 /*
221 * See if the PCI bus has been configured by the firmware.
222 */
Ralf Baechle8fb303c2007-03-24 14:26:13 +0000223 reg = __raw_readq(IOADDR(A_SCD_SYSTEM_CFG));
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700224 if (!(reg & M_BCM1480_SYS_PCI_HOST)) {
225 bcm1480_bus_status |= PCI_DEVICE_MODE;
226 } else {
227 cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0),
228 PCI_COMMAND));
229 if (!(cmdreg & PCI_COMMAND_MASTER)) {
230 printk
Ralf Baechle70342282013-01-22 12:59:30 +0100231 ("PCI: Skipping PCI probe. Bus is not initialized.\n");
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700232 iounmap(cfg_space);
233 return 1; /* XXX */
234 }
235 bcm1480_bus_status |= PCI_BUS_ENABLED;
236 }
237
Andrew Isaacsoncb426242005-10-19 23:59:46 -0700238 /* turn on ExpMemEn */
239 cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40));
Andrew Isaacsoncb426242005-10-19 23:59:46 -0700240 WRITECFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40),
241 cmdreg | 0x10);
242 cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40));
Andrew Isaacsoncb426242005-10-19 23:59:46 -0700243
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700244 /*
245 * Establish mappings in KSEG2 (kernel virtual) to PCI I/O
246 * space. Use "match bytes" policy to make everything look
247 * little-endian. So, you need to also set
248 * CONFIG_SWAP_IO_SPACE, but this is the combination that
249 * works correctly with most of Linux's drivers.
250 * XXX ehs: Should this happen in PCI Device mode?
251 */
252
Thomas Bogendoerfere790a462008-03-08 19:51:55 +0100253 bcm1480_controller.io_map_base = (unsigned long)
254 ioremap(A_BCM1480_PHYS_PCI_IO_MATCH_BYTES, 65536);
Thomas Bogendoerfercf7b7e02008-03-16 18:14:16 +0100255 bcm1480_controller.io_map_base -= bcm1480_controller.io_offset;
Thomas Bogendoerfere790a462008-03-08 19:51:55 +0100256 set_io_port_base(bcm1480_controller.io_map_base);
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700257
258 register_pci_controller(&bcm1480_controller);
259
260#ifdef CONFIG_VGA_CONSOLE
Wang YanQing155957f2013-05-21 13:15:12 +0800261 console_lock();
262 do_take_over_console(&vga_con, 0, MAX_NR_CONSOLES-1, 1);
263 console_unlock();
Andrew Isaacsondc41f942005-10-19 23:58:49 -0700264#endif
265 return 0;
266}
267
268arch_initcall(bcm1480_pcibios_init);