2 * arch/arm/plat-orion/pcie.c
4 * Marvell Orion SoC PCIe handling.
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.
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/mbus.h>
14 #include <asm/mach/pci.h>
15 #include <asm/plat-orion/pcie.h>
18 * PCIe unit register offsets.
20 #define PCIE_DEV_ID_OFF 0x0000
21 #define PCIE_CMD_OFF 0x0004
22 #define PCIE_DEV_REV_OFF 0x0008
23 #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
24 #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
25 #define PCIE_HEADER_LOG_4_OFF 0x0128
26 #define PCIE_BAR_CTRL_OFF(n) (0x1804 + ((n - 1) * 4))
27 #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
28 #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
29 #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
30 #define PCIE_WIN5_CTRL_OFF 0x1880
31 #define PCIE_WIN5_BASE_OFF 0x1884
32 #define PCIE_WIN5_REMAP_OFF 0x188c
33 #define PCIE_CONF_ADDR_OFF 0x18f8
34 #define PCIE_CONF_ADDR_EN 0x80000000
35 #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
36 #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
37 #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
38 #define PCIE_CONF_FUNC(f) (((f) & 0x3) << 8)
39 #define PCIE_CONF_DATA_OFF 0x18fc
40 #define PCIE_MASK_OFF 0x1910
41 #define PCIE_CTRL_OFF 0x1a00
42 #define PCIE_STAT_OFF 0x1a04
43 #define PCIE_STAT_DEV_OFFS 20
44 #define PCIE_STAT_DEV_MASK 0x1f
45 #define PCIE_STAT_BUS_OFFS 8
46 #define PCIE_STAT_BUS_MASK 0xff
47 #define PCIE_STAT_LINK_DOWN 1
50 u32 __init orion_pcie_dev_id(void __iomem *base)
52 return readl(base + PCIE_DEV_ID_OFF) >> 16;
55 u32 __init orion_pcie_rev(void __iomem *base)
57 return readl(base + PCIE_DEV_REV_OFF) & 0xff;
60 int orion_pcie_link_up(void __iomem *base)
62 return !(readl(base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
65 int orion_pcie_get_local_bus_nr(void __iomem *base)
67 u32 stat = readl(base + PCIE_STAT_OFF);
69 return (stat >> PCIE_STAT_BUS_OFFS) & PCIE_STAT_BUS_MASK;
72 void __init orion_pcie_set_local_bus_nr(void __iomem *base, int nr)
76 stat = readl(base + PCIE_STAT_OFF);
77 stat &= ~(PCIE_STAT_BUS_MASK << PCIE_STAT_BUS_OFFS);
78 stat |= nr << PCIE_STAT_BUS_OFFS;
79 writel(stat, base + PCIE_STAT_OFF);
83 * Setup PCIE BARs and Address Decode Wins:
84 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
85 * WIN[0-3] -> DRAM bank[0-3]
87 static void __init orion_pcie_setup_wins(void __iomem *base,
88 struct mbus_dram_target_info *dram)
94 * First, disable and clear BARs and windows.
96 for (i = 1; i <= 2; i++) {
97 writel(0, base + PCIE_BAR_CTRL_OFF(i));
98 writel(0, base + PCIE_BAR_LO_OFF(i));
99 writel(0, base + PCIE_BAR_HI_OFF(i));
102 for (i = 0; i < 5; i++) {
103 writel(0, base + PCIE_WIN04_CTRL_OFF(i));
104 writel(0, base + PCIE_WIN04_BASE_OFF(i));
105 writel(0, base + PCIE_WIN04_REMAP_OFF(i));
108 writel(0, base + PCIE_WIN5_CTRL_OFF);
109 writel(0, base + PCIE_WIN5_BASE_OFF);
110 writel(0, base + PCIE_WIN5_REMAP_OFF);
113 * Setup windows for DDR banks. Count total DDR size on the fly.
116 for (i = 0; i < dram->num_cs; i++) {
117 struct mbus_dram_window *cs = dram->cs + i;
119 writel(cs->base & 0xffff0000, base + PCIE_WIN04_BASE_OFF(i));
120 writel(0, base + PCIE_WIN04_REMAP_OFF(i));
121 writel(((cs->size - 1) & 0xffff0000) |
122 (cs->mbus_attr << 8) |
123 (dram->mbus_dram_target_id << 4) | 1,
124 base + PCIE_WIN04_CTRL_OFF(i));
130 * Setup BAR[1] to all DRAM banks.
132 writel(dram->cs[0].base, base + PCIE_BAR_LO_OFF(1));
133 writel(0, base + PCIE_BAR_HI_OFF(1));
134 writel(((size - 1) & 0xffff0000) | 1, base + PCIE_BAR_CTRL_OFF(1));
137 void __init orion_pcie_setup(void __iomem *base,
138 struct mbus_dram_target_info *dram)
144 * Point PCIe unit MBUS decode windows to DRAM space.
146 orion_pcie_setup_wins(base, dram);
149 * Master + slave enable.
151 cmd = readw(base + PCIE_CMD_OFF);
152 cmd |= PCI_COMMAND_IO;
153 cmd |= PCI_COMMAND_MEMORY;
154 cmd |= PCI_COMMAND_MASTER;
155 writew(cmd, base + PCIE_CMD_OFF);
158 * Enable interrupt lines A-D.
160 mask = readl(base + PCIE_MASK_OFF);
162 writel(mask, base + PCIE_MASK_OFF);
165 int orion_pcie_rd_conf(void __iomem *base, struct pci_bus *bus,
166 u32 devfn, int where, int size, u32 *val)
168 writel(PCIE_CONF_BUS(bus->number) |
169 PCIE_CONF_DEV(PCI_SLOT(devfn)) |
170 PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
171 PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN,
172 base + PCIE_CONF_ADDR_OFF);
174 *val = readl(base + PCIE_CONF_DATA_OFF);
177 *val = (*val >> (8 * (where & 3))) & 0xff;
179 *val = (*val >> (8 * (where & 3))) & 0xffff;
181 return PCIBIOS_SUCCESSFUL;
184 int orion_pcie_rd_conf_tlp(void __iomem *base, struct pci_bus *bus,
185 u32 devfn, int where, int size, u32 *val)
187 writel(PCIE_CONF_BUS(bus->number) |
188 PCIE_CONF_DEV(PCI_SLOT(devfn)) |
189 PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
190 PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN,
191 base + PCIE_CONF_ADDR_OFF);
193 *val = readl(base + PCIE_CONF_DATA_OFF);
195 if (bus->number != orion_pcie_get_local_bus_nr(base) ||
196 PCI_FUNC(devfn) != 0)
197 *val = readl(base + PCIE_HEADER_LOG_4_OFF);
200 *val = (*val >> (8 * (where & 3))) & 0xff;
202 *val = (*val >> (8 * (where & 3))) & 0xffff;
204 return PCIBIOS_SUCCESSFUL;
207 int orion_pcie_rd_conf_wa(void __iomem *wa_base, struct pci_bus *bus,
208 u32 devfn, int where, int size, u32 *val)
210 *val = readl(wa_base + (PCIE_CONF_BUS(bus->number) |
211 PCIE_CONF_DEV(PCI_SLOT(devfn)) |
212 PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
213 PCIE_CONF_REG(where)));
216 *val = (*val >> (8 * (where & 3))) & 0xff;
218 *val = (*val >> (8 * (where & 3))) & 0xffff;
220 return PCIBIOS_SUCCESSFUL;
223 int orion_pcie_wr_conf(void __iomem *base, struct pci_bus *bus,
224 u32 devfn, int where, int size, u32 val)
226 int ret = PCIBIOS_SUCCESSFUL;
228 writel(PCIE_CONF_BUS(bus->number) |
229 PCIE_CONF_DEV(PCI_SLOT(devfn)) |
230 PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
231 PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN,
232 base + PCIE_CONF_ADDR_OFF);
235 writel(val, base + PCIE_CONF_DATA_OFF);
236 } else if (size == 2) {
237 writew(val, base + PCIE_CONF_DATA_OFF + (where & 3));
238 } else if (size == 1) {
239 writeb(val, base + PCIE_CONF_DATA_OFF + (where & 3));
241 ret = PCIBIOS_BAD_REGISTER_NUMBER;