blob: bec933b04ef04bebcac7d312b46f710b9ce3a9e3 [file] [log] [blame]
Catalin Marinasc0da0852005-06-20 18:51:06 +01001/*
2 * linux/arch/arm/mach-versatile/pci.c
3 *
4 * (C) Copyright Koninklijke Philips Electronics NV 2004. All rights reserved.
5 * You can redistribute and/or modify this software under the terms of version 2
6 * of the GNU General Public License as published by the Free Software Foundation.
7 * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
8 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
10 * Koninklijke Philips Electronics nor its subsidiaries is obligated to provide any support for this software.
11 *
12 * ARM Versatile PCI driver.
13 *
14 * 14/04/2005 Initial version, colin.king@philips.com
15 *
16 */
Catalin Marinasc0da0852005-06-20 18:51:06 +010017#include <linux/kernel.h>
18#include <linux/pci.h>
Catalin Marinasc0da0852005-06-20 18:51:06 +010019#include <linux/ioport.h>
20#include <linux/interrupt.h>
21#include <linux/spinlock.h>
22#include <linux/init.h>
Russell Kingfced80c2008-09-06 12:10:45 +010023#include <linux/io.h>
Catalin Marinasc0da0852005-06-20 18:51:06 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Catalin Marinasc0da0852005-06-20 18:51:06 +010026#include <asm/irq.h>
Catalin Marinasc0da0852005-06-20 18:51:06 +010027#include <asm/mach/pci.h>
Catalin Marinasc0da0852005-06-20 18:51:06 +010028
29/*
30 * these spaces are mapped using the following base registers:
31 *
32 * Usage Local Bus Memory Base/Map registers used
33 *
34 * Mem 50000000 - 5FFFFFFF LB_BASE0/LB_MAP0, non prefetch
35 * Mem 60000000 - 6FFFFFFF LB_BASE1/LB_MAP1, prefetch
36 * IO 44000000 - 4FFFFFFF LB_BASE2/LB_MAP2, IO
37 * Cfg 42000000 - 42FFFFFF PCI config
38 *
39 */
Al Viro399ad772006-10-11 17:22:34 +010040#define __IO_ADDRESS(n) ((void __iomem *)(unsigned long)IO_ADDRESS(n))
41#define SYS_PCICTL __IO_ADDRESS(VERSATILE_SYS_PCICTL)
42#define PCI_IMAP0 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x0)
43#define PCI_IMAP1 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x4)
44#define PCI_IMAP2 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x8)
45#define PCI_SMAP0 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x10)
46#define PCI_SMAP1 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x14)
47#define PCI_SMAP2 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x18)
48#define PCI_SELFID __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0xc)
Catalin Marinasc0da0852005-06-20 18:51:06 +010049
50#define DEVICE_ID_OFFSET 0x00
51#define CSR_OFFSET 0x04
52#define CLASS_ID_OFFSET 0x08
53
54#define VP_PCI_DEVICE_ID 0x030010ee
55#define VP_PCI_CLASS_ID 0x0b400000
56
57static unsigned long pci_slot_ignore = 0;
58
59static int __init versatile_pci_slot_ignore(char *str)
60{
61 int retval;
62 int slot;
63
64 while ((retval = get_option(&str,&slot))) {
65 if ((slot < 0) || (slot > 31)) {
66 printk("Illegal slot value: %d\n",slot);
67 } else {
68 pci_slot_ignore |= (1 << slot);
69 }
70 }
71 return 1;
72}
73
74__setup("pci_slot_ignore=", versatile_pci_slot_ignore);
75
76
Al Viro399ad772006-10-11 17:22:34 +010077static void __iomem *__pci_addr(struct pci_bus *bus,
Catalin Marinasc0da0852005-06-20 18:51:06 +010078 unsigned int devfn, int offset)
79{
80 unsigned int busnr = bus->number;
81
82 /*
83 * Trap out illegal values
84 */
85 if (offset > 255)
86 BUG();
87 if (busnr > 255)
88 BUG();
89 if (devfn > 255)
90 BUG();
91
Al Viro399ad772006-10-11 17:22:34 +010092 return VERSATILE_PCI_CFG_VIRT_BASE + ((busnr << 16) |
Catalin Marinasc0da0852005-06-20 18:51:06 +010093 (PCI_SLOT(devfn) << 11) | (PCI_FUNC(devfn) << 8) | offset);
94}
95
96static int versatile_read_config(struct pci_bus *bus, unsigned int devfn, int where,
97 int size, u32 *val)
98{
Al Viro399ad772006-10-11 17:22:34 +010099 void __iomem *addr = __pci_addr(bus, devfn, where & ~3);
Catalin Marinasc0da0852005-06-20 18:51:06 +0100100 u32 v;
101 int slot = PCI_SLOT(devfn);
102
103 if (pci_slot_ignore & (1 << slot)) {
104 /* Ignore this slot */
105 switch (size) {
106 case 1:
107 v = 0xff;
108 break;
109 case 2:
110 v = 0xffff;
111 break;
112 default:
113 v = 0xffffffff;
114 }
115 } else {
116 switch (size) {
117 case 1:
Andrzej Zaborowski756813c2007-06-26 14:31:23 +0100118 v = __raw_readl(addr);
119 if (where & 2) v >>= 16;
120 if (where & 1) v >>= 8;
121 v &= 0xff;
Catalin Marinasc0da0852005-06-20 18:51:06 +0100122 break;
123
124 case 2:
Al Viro399ad772006-10-11 17:22:34 +0100125 v = __raw_readl(addr);
126 if (where & 2) v >>= 16;
Catalin Marinasc0da0852005-06-20 18:51:06 +0100127 v &= 0xffff;
128 break;
129
130 default:
Catalin Marinasc0da0852005-06-20 18:51:06 +0100131 v = __raw_readl(addr);
132 break;
133 }
134 }
135
136 *val = v;
137 return PCIBIOS_SUCCESSFUL;
138}
139
140static int versatile_write_config(struct pci_bus *bus, unsigned int devfn, int where,
141 int size, u32 val)
142{
Al Viro399ad772006-10-11 17:22:34 +0100143 void __iomem *addr = __pci_addr(bus, devfn, where);
Catalin Marinasc0da0852005-06-20 18:51:06 +0100144 int slot = PCI_SLOT(devfn);
145
146 if (pci_slot_ignore & (1 << slot)) {
147 return PCIBIOS_SUCCESSFUL;
148 }
149
150 switch (size) {
151 case 1:
152 __raw_writeb((u8)val, addr);
153 break;
154
155 case 2:
156 __raw_writew((u16)val, addr);
157 break;
158
159 case 4:
160 __raw_writel(val, addr);
161 break;
162 }
163
164 return PCIBIOS_SUCCESSFUL;
165}
166
167static struct pci_ops pci_versatile_ops = {
168 .read = versatile_read_config,
169 .write = versatile_write_config,
170};
171
Arnd Bergmann9b0f7e32012-06-11 09:03:58 -0500172static struct resource io_port = {
173 .name = "PCI",
174 .start = 0,
175 .end = IO_SPACE_LIMIT,
176 .flags = IORESOURCE_IO,
177};
178
Catalin Marinasc0da0852005-06-20 18:51:06 +0100179static struct resource io_mem = {
180 .name = "PCI I/O space",
181 .start = VERSATILE_PCI_MEM_BASE0,
182 .end = VERSATILE_PCI_MEM_BASE0+VERSATILE_PCI_MEM_BASE0_SIZE-1,
Arnd Bergmann9b0f7e32012-06-11 09:03:58 -0500183 .flags = IORESOURCE_MEM,
Catalin Marinasc0da0852005-06-20 18:51:06 +0100184};
185
186static struct resource non_mem = {
187 .name = "PCI non-prefetchable",
188 .start = VERSATILE_PCI_MEM_BASE1,
189 .end = VERSATILE_PCI_MEM_BASE1+VERSATILE_PCI_MEM_BASE1_SIZE-1,
190 .flags = IORESOURCE_MEM,
191};
192
193static struct resource pre_mem = {
194 .name = "PCI prefetchable",
195 .start = VERSATILE_PCI_MEM_BASE2,
196 .end = VERSATILE_PCI_MEM_BASE2+VERSATILE_PCI_MEM_BASE2_SIZE-1,
197 .flags = IORESOURCE_MEM | IORESOURCE_PREFETCH,
198};
199
Paul Gortmakeree5324e2012-04-02 19:48:25 -0400200static int __init pci_versatile_setup_resources(struct pci_sys_data *sys)
Catalin Marinasc0da0852005-06-20 18:51:06 +0100201{
202 int ret = 0;
203
204 ret = request_resource(&iomem_resource, &io_mem);
205 if (ret) {
206 printk(KERN_ERR "PCI: unable to allocate I/O "
207 "memory region (%d)\n", ret);
208 goto out;
209 }
Arnd Bergmann9b0f7e32012-06-11 09:03:58 -0500210 ret = request_resource(&ioport_resource, &io_port);
211 if (ret) {
212 printk(KERN_ERR "PCI: unable to allocate I/O "
213 "port region (%d)\n", ret);
214 goto out;
215 }
Catalin Marinasc0da0852005-06-20 18:51:06 +0100216 ret = request_resource(&iomem_resource, &non_mem);
217 if (ret) {
218 printk(KERN_ERR "PCI: unable to allocate non-prefetchable "
219 "memory region (%d)\n", ret);
220 goto release_io_mem;
221 }
222 ret = request_resource(&iomem_resource, &pre_mem);
223 if (ret) {
224 printk(KERN_ERR "PCI: unable to allocate prefetchable "
225 "memory region (%d)\n", ret);
226 goto release_non_mem;
227 }
228
229 /*
Bjorn Helgaas37d15902011-10-28 16:26:16 -0600230 * the IO resource for this bus
231 * the mem resource for this bus
232 * the prefetch mem resource for this bus
Catalin Marinasc0da0852005-06-20 18:51:06 +0100233 */
Arnd Bergmann9b0f7e32012-06-11 09:03:58 -0500234 pci_add_resource_offset(&sys->resources, &io_port, sys->io_offset);
Paul Gortmakeree5324e2012-04-02 19:48:25 -0400235 pci_add_resource_offset(&sys->resources, &non_mem, sys->mem_offset);
236 pci_add_resource_offset(&sys->resources, &pre_mem, sys->mem_offset);
Catalin Marinasc0da0852005-06-20 18:51:06 +0100237
238 goto out;
239
240 release_non_mem:
241 release_resource(&non_mem);
242 release_io_mem:
243 release_resource(&io_mem);
244 out:
245 return ret;
246}
247
248int __init pci_versatile_setup(int nr, struct pci_sys_data *sys)
249{
250 int ret = 0;
251 int i;
252 int myslot = -1;
253 unsigned long val;
Catalin Marinasc27a2162006-02-22 19:51:38 +0000254 void __iomem *local_pci_cfg_base;
255
256 val = __raw_readl(SYS_PCICTL);
257 if (!(val & 1)) {
258 printk("Not plugged into PCI backplane!\n");
259 ret = -EIO;
260 goto out;
261 }
Catalin Marinasc0da0852005-06-20 18:51:06 +0100262
263 if (nr == 0) {
264 sys->mem_offset = 0;
Arnd Bergmann9b0f7e32012-06-11 09:03:58 -0500265 sys->io_offset = 0;
Paul Gortmakeree5324e2012-04-02 19:48:25 -0400266 ret = pci_versatile_setup_resources(sys);
Catalin Marinasc0da0852005-06-20 18:51:06 +0100267 if (ret < 0) {
268 printk("pci_versatile_setup: resources... oops?\n");
269 goto out;
270 }
271 } else {
272 printk("pci_versatile_setup: resources... nr == 0??\n");
273 goto out;
274 }
275
Catalin Marinasc0da0852005-06-20 18:51:06 +0100276 /*
277 * We need to discover the PCI core first to configure itself
278 * before the main PCI probing is performed
279 */
Catalin Marinasc27a2162006-02-22 19:51:38 +0000280 for (i=0; i<32; i++)
Catalin Marinasc0da0852005-06-20 18:51:06 +0100281 if ((__raw_readl(VERSATILE_PCI_VIRT_BASE+(i<<11)+DEVICE_ID_OFFSET) == VP_PCI_DEVICE_ID) &&
282 (__raw_readl(VERSATILE_PCI_VIRT_BASE+(i<<11)+CLASS_ID_OFFSET) == VP_PCI_CLASS_ID)) {
283 myslot = i;
Catalin Marinasc0da0852005-06-20 18:51:06 +0100284 break;
285 }
Catalin Marinasc0da0852005-06-20 18:51:06 +0100286
287 if (myslot == -1) {
288 printk("Cannot find PCI core!\n");
289 ret = -EIO;
Catalin Marinasc27a2162006-02-22 19:51:38 +0000290 goto out;
Catalin Marinasc0da0852005-06-20 18:51:06 +0100291 }
292
Catalin Marinasc27a2162006-02-22 19:51:38 +0000293 printk("PCI core found (slot %d)\n",myslot);
294
295 __raw_writel(myslot, PCI_SELFID);
Al Viro399ad772006-10-11 17:22:34 +0100296 local_pci_cfg_base = VERSATILE_PCI_CFG_VIRT_BASE + (myslot << 11);
Catalin Marinasc27a2162006-02-22 19:51:38 +0000297
298 val = __raw_readl(local_pci_cfg_base + CSR_OFFSET);
299 val |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
300 __raw_writel(val, local_pci_cfg_base + CSR_OFFSET);
301
302 /*
303 * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM
304 */
305 __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_0);
306 __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_1);
307 __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_2);
308
309 /*
310 * Do not to map Versatile FPGA PCI device into memory space
311 */
312 pci_slot_ignore |= (1 << myslot);
313 ret = 1;
314
Catalin Marinasc0da0852005-06-20 18:51:06 +0100315 out:
316 return ret;
317}
318
319
Catalin Marinasc0da0852005-06-20 18:51:06 +0100320void __init pci_versatile_preinit(void)
321{
Rob Herringc9d95fb2011-06-28 21:16:13 -0500322 pcibios_min_io = 0x44000000;
323 pcibios_min_mem = 0x50000000;
324
Catalin Marinasc27a2162006-02-22 19:51:38 +0000325 __raw_writel(VERSATILE_PCI_MEM_BASE0 >> 28, PCI_IMAP0);
326 __raw_writel(VERSATILE_PCI_MEM_BASE1 >> 28, PCI_IMAP1);
327 __raw_writel(VERSATILE_PCI_MEM_BASE2 >> 28, PCI_IMAP2);
Catalin Marinasc0da0852005-06-20 18:51:06 +0100328
Catalin Marinasc27a2162006-02-22 19:51:38 +0000329 __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP0);
330 __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP1);
331 __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP2);
Catalin Marinasc0da0852005-06-20 18:51:06 +0100332
Catalin Marinasc27a2162006-02-22 19:51:38 +0000333 __raw_writel(1, SYS_PCICTL);
334}
Catalin Marinasc0da0852005-06-20 18:51:06 +0100335
336/*
337 * map the specified device/slot/pin to an IRQ. Different backplanes may need to modify this.
338 */
Ralf Baechled5341942011-06-10 15:30:21 +0100339static int __init versatile_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Catalin Marinasc0da0852005-06-20 18:51:06 +0100340{
341 int irq;
342 int devslot = PCI_SLOT(dev->devfn);
343
Catalin Marinasc27a2162006-02-22 19:51:38 +0000344 /* slot, pin, irq
345 * 24 1 27
346 * 25 1 28
347 * 26 1 29
348 * 27 1 30
349 */
Russell King1bc39ac2012-03-10 11:32:34 +0000350 irq = 27 + ((slot - 24 + pin - 1) & 3);
Catalin Marinasc0da0852005-06-20 18:51:06 +0100351
352 return irq;
353}
354
355static struct hw_pci versatile_pci __initdata = {
Catalin Marinasc0da0852005-06-20 18:51:06 +0100356 .map_irq = versatile_map_irq,
357 .nr_controllers = 1,
Russell Kingc23bfc32012-03-10 12:49:16 +0000358 .ops = &pci_versatile_ops,
Catalin Marinasc0da0852005-06-20 18:51:06 +0100359 .setup = pci_versatile_setup,
Catalin Marinasc0da0852005-06-20 18:51:06 +0100360 .preinit = pci_versatile_preinit,
Catalin Marinasc0da0852005-06-20 18:51:06 +0100361};
362
363static int __init versatile_pci_init(void)
364{
365 pci_common_init(&versatile_pci);
366 return 0;
367}
368
369subsys_initcall(versatile_pci_init);