blob: f6b08f207c894189d49bb52fec1de1b2bf65c457 [file] [log] [blame]
Saeed Bishara651c74c2008-06-22 22:45:06 +02001/*
2 * arch/arm/mach-kirkwood/pcie.c
3 *
4 * PCIe functions for Marvell Kirkwood SoCs
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#include <linux/kernel.h>
12#include <linux/pci.h>
13#include <linux/mbus.h>
14#include <asm/mach/pci.h>
Lennert Buytenhek6f088f12008-08-09 13:44:58 +020015#include <plat/pcie.h>
Saeed Bishara651c74c2008-06-22 22:45:06 +020016#include "common.h"
17
18
19#define PCIE_BASE ((void __iomem *)PCIE_VIRT_BASE)
20
Ronen Shitritb2b3dc22008-09-15 10:40:35 +030021void __init kirkwood_pcie_id(u32 *dev, u32 *rev)
22{
23 *dev = orion_pcie_dev_id(PCIE_BASE);
24 *rev = orion_pcie_rev(PCIE_BASE);
25}
26
Saeed Bishara651c74c2008-06-22 22:45:06 +020027static int pcie_valid_config(int bus, int dev)
28{
29 /*
30 * Don't go out when trying to access --
31 * 1. nonexisting device on local bus
32 * 2. where there's no device connected (no link)
33 */
34 if (bus == 0 && dev == 0)
35 return 1;
36
37 if (!orion_pcie_link_up(PCIE_BASE))
38 return 0;
39
40 if (bus == 0 && dev != 1)
41 return 0;
42
43 return 1;
44}
45
46
47/*
48 * PCIe config cycles are done by programming the PCIE_CONF_ADDR register
49 * and then reading the PCIE_CONF_DATA register. Need to make sure these
50 * transactions are atomic.
51 */
52static DEFINE_SPINLOCK(kirkwood_pcie_lock);
53
54static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
55 int size, u32 *val)
56{
57 unsigned long flags;
58 int ret;
59
60 if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0) {
61 *val = 0xffffffff;
62 return PCIBIOS_DEVICE_NOT_FOUND;
63 }
64
65 spin_lock_irqsave(&kirkwood_pcie_lock, flags);
66 ret = orion_pcie_rd_conf(PCIE_BASE, bus, devfn, where, size, val);
67 spin_unlock_irqrestore(&kirkwood_pcie_lock, flags);
68
69 return ret;
70}
71
72static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
73 int where, int size, u32 val)
74{
75 unsigned long flags;
76 int ret;
77
78 if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0)
79 return PCIBIOS_DEVICE_NOT_FOUND;
80
81 spin_lock_irqsave(&kirkwood_pcie_lock, flags);
82 ret = orion_pcie_wr_conf(PCIE_BASE, bus, devfn, where, size, val);
83 spin_unlock_irqrestore(&kirkwood_pcie_lock, flags);
84
85 return ret;
86}
87
88static struct pci_ops pcie_ops = {
89 .read = pcie_rd_conf,
90 .write = pcie_wr_conf,
91};
92
93
94static int kirkwood_pcie_setup(int nr, struct pci_sys_data *sys)
95{
96 struct resource *res;
97
98 /*
99 * Generic PCIe unit setup.
100 */
101 orion_pcie_setup(PCIE_BASE, &kirkwood_mbus_dram_info);
102
103 /*
104 * Request resources.
105 */
106 res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
107 if (!res)
108 panic("pcie_setup unable to alloc resources");
109
110 /*
111 * IORESOURCE_IO
112 */
113 res[0].name = "PCIe I/O Space";
114 res[0].flags = IORESOURCE_IO;
115 res[0].start = KIRKWOOD_PCIE_IO_PHYS_BASE;
116 res[0].end = res[0].start + KIRKWOOD_PCIE_IO_SIZE - 1;
117 if (request_resource(&ioport_resource, &res[0]))
118 panic("Request PCIe IO resource failed\n");
119 sys->resource[0] = &res[0];
120
121 /*
122 * IORESOURCE_MEM
123 */
124 res[1].name = "PCIe Memory Space";
125 res[1].flags = IORESOURCE_MEM;
126 res[1].start = KIRKWOOD_PCIE_MEM_PHYS_BASE;
127 res[1].end = res[1].start + KIRKWOOD_PCIE_MEM_SIZE - 1;
128 if (request_resource(&iomem_resource, &res[1]))
129 panic("Request PCIe Memory resource failed\n");
130 sys->resource[1] = &res[1];
131
132 sys->resource[2] = NULL;
133 sys->io_offset = 0;
134
135 return 1;
136}
137
138static void __devinit rc_pci_fixup(struct pci_dev *dev)
139{
140 /*
141 * Prevent enumeration of root complex.
142 */
143 if (dev->bus->parent == NULL && dev->devfn == 0) {
144 int i;
145
146 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
147 dev->resource[i].start = 0;
148 dev->resource[i].end = 0;
149 dev->resource[i].flags = 0;
150 }
151 }
152}
153DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
154
155static struct pci_bus __init *
156kirkwood_pcie_scan_bus(int nr, struct pci_sys_data *sys)
157{
158 struct pci_bus *bus;
159
160 if (nr == 0) {
161 bus = pci_scan_bus(sys->busnr, &pcie_ops, sys);
162 } else {
163 bus = NULL;
164 BUG();
165 }
166
167 return bus;
168}
169
170static int __init kirkwood_pcie_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
171{
172 return IRQ_KIRKWOOD_PCIE;
173}
174
175static struct hw_pci kirkwood_pci __initdata = {
176 .nr_controllers = 1,
177 .swizzle = pci_std_swizzle,
178 .setup = kirkwood_pcie_setup,
179 .scan = kirkwood_pcie_scan_bus,
180 .map_irq = kirkwood_pcie_map_irq,
181};
182
183void __init kirkwood_pcie_init(void)
184{
185 pci_common_init(&kirkwood_pci);
186}