blob: 7ed8096766422759cf333062020c57f157566687 [file] [log] [blame]
Paul Mackerrasdaec9622005-10-10 22:25:26 +10001/*
2 * Support for indirect PCI bridges.
3 *
4 * Copyright (C) 1998 Gabriel Paubert.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/pci.h>
14#include <linux/delay.h>
15#include <linux/string.h>
16#include <linux/init.h>
17
18#include <asm/io.h>
19#include <asm/prom.h>
20#include <asm/pci-bridge.h>
21#include <asm/machdep.h>
22
Paul Mackerrasdaec9622005-10-10 22:25:26 +100023static int
24indirect_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
25 int len, u32 *val)
26{
Kumar Gala19afa402009-04-30 03:10:07 +000027 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasdaec9622005-10-10 22:25:26 +100028 volatile void __iomem *cfg_data;
29 u8 cfg_type = 0;
Kumar Galaab0f9ad2007-06-25 15:19:48 -050030 u32 bus_no, reg;
Paul Mackerrasdaec9622005-10-10 22:25:26 +100031
Kumar Gala62c66c82007-07-11 13:22:41 -050032 if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK) {
33 if (bus->number != hose->first_busno)
34 return PCIBIOS_DEVICE_NOT_FOUND;
35 if (devfn != 0)
36 return PCIBIOS_DEVICE_NOT_FOUND;
37 }
38
Paul Mackerrasdaec9622005-10-10 22:25:26 +100039 if (ppc_md.pci_exclude_device)
Kumar Gala7d52c7b2007-06-22 00:23:57 -050040 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
Paul Mackerrasdaec9622005-10-10 22:25:26 +100041 return PCIBIOS_DEVICE_NOT_FOUND;
Kumar Gala62c66c82007-07-11 13:22:41 -050042
Kumar Galaab0f9ad2007-06-25 15:19:48 -050043 if (hose->indirect_type & PPC_INDIRECT_TYPE_SET_CFG_TYPE)
Paul Mackerrasdaec9622005-10-10 22:25:26 +100044 if (bus->number != hose->first_busno)
45 cfg_type = 1;
46
Kumar Gala5ab65ec2007-06-25 13:09:42 -050047 bus_no = (bus->number == hose->first_busno) ?
Kumar Gala0a3786c2007-06-25 13:32:48 -050048 hose->self_busno : bus->number;
Kumar Gala5ab65ec2007-06-25 13:09:42 -050049
Kumar Galaab0f9ad2007-06-25 15:19:48 -050050 if (hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG)
51 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
52 else
53 reg = offset & 0xfc;
54
Kumar Gala2e56ff22007-07-19 16:07:35 -050055 if (hose->indirect_type & PPC_INDIRECT_TYPE_BIG_ENDIAN)
56 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
57 (devfn << 8) | reg | cfg_type));
58 else
59 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
60 (devfn << 8) | reg | cfg_type));
Paul Mackerrasdaec9622005-10-10 22:25:26 +100061
62 /*
63 * Note: the caller has already checked that offset is
64 * suitably aligned and that len is 1, 2 or 4.
65 */
66 cfg_data = hose->cfg_data + (offset & 3);
67 switch (len) {
68 case 1:
69 *val = in_8(cfg_data);
70 break;
71 case 2:
72 *val = in_le16(cfg_data);
73 break;
74 default:
75 *val = in_le32(cfg_data);
76 break;
77 }
78 return PCIBIOS_SUCCESSFUL;
79}
80
81static int
82indirect_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
83 int len, u32 val)
84{
Kumar Gala19afa402009-04-30 03:10:07 +000085 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasdaec9622005-10-10 22:25:26 +100086 volatile void __iomem *cfg_data;
87 u8 cfg_type = 0;
Kumar Galaab0f9ad2007-06-25 15:19:48 -050088 u32 bus_no, reg;
Paul Mackerrasdaec9622005-10-10 22:25:26 +100089
Kumar Gala62c66c82007-07-11 13:22:41 -050090 if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK) {
91 if (bus->number != hose->first_busno)
92 return PCIBIOS_DEVICE_NOT_FOUND;
93 if (devfn != 0)
94 return PCIBIOS_DEVICE_NOT_FOUND;
95 }
96
Paul Mackerrasdaec9622005-10-10 22:25:26 +100097 if (ppc_md.pci_exclude_device)
Kumar Gala7d52c7b2007-06-22 00:23:57 -050098 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
Paul Mackerrasdaec9622005-10-10 22:25:26 +100099 return PCIBIOS_DEVICE_NOT_FOUND;
100
Kumar Galaab0f9ad2007-06-25 15:19:48 -0500101 if (hose->indirect_type & PPC_INDIRECT_TYPE_SET_CFG_TYPE)
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000102 if (bus->number != hose->first_busno)
103 cfg_type = 1;
104
Kumar Gala5ab65ec2007-06-25 13:09:42 -0500105 bus_no = (bus->number == hose->first_busno) ?
Kumar Gala0a3786c2007-06-25 13:32:48 -0500106 hose->self_busno : bus->number;
Kumar Gala5ab65ec2007-06-25 13:09:42 -0500107
Kumar Galaab0f9ad2007-06-25 15:19:48 -0500108 if (hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG)
109 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
110 else
111 reg = offset & 0xfc;
112
Kumar Gala2e56ff22007-07-19 16:07:35 -0500113 if (hose->indirect_type & PPC_INDIRECT_TYPE_BIG_ENDIAN)
114 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
115 (devfn << 8) | reg | cfg_type));
116 else
117 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
118 (devfn << 8) | reg | cfg_type));
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000119
Kumar Gala476f5772007-06-26 12:12:55 -0500120 /* surpress setting of PCI_PRIMARY_BUS */
121 if (hose->indirect_type & PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS)
122 if ((offset == PCI_PRIMARY_BUS) &&
123 (bus->number == hose->first_busno))
124 val &= 0xffffff00;
125
Josh Boyer5ce4b592008-06-17 19:01:38 -0400126 /* Workaround for PCI_28 Errata in 440EPx/GRx */
127 if ((hose->indirect_type & PPC_INDIRECT_TYPE_BROKEN_MRM) &&
128 offset == PCI_CACHE_LINE_SIZE) {
129 val = 0;
130 }
131
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000132 /*
133 * Note: the caller has already checked that offset is
134 * suitably aligned and that len is 1, 2 or 4.
135 */
136 cfg_data = hose->cfg_data + (offset & 3);
137 switch (len) {
138 case 1:
139 out_8(cfg_data, val);
140 break;
141 case 2:
142 out_le16(cfg_data, val);
143 break;
144 default:
145 out_le32(cfg_data, val);
146 break;
147 }
148 return PCIBIOS_SUCCESSFUL;
149}
150
151static struct pci_ops indirect_pci_ops =
152{
Nathan Lynchc78d4532007-08-10 05:18:45 +1000153 .read = indirect_read_config,
154 .write = indirect_write_config,
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000155};
156
157void __init
Valentine Barshakd94bad82007-10-08 22:51:24 +1000158setup_indirect_pci(struct pci_controller* hose,
159 resource_size_t cfg_addr,
160 resource_size_t cfg_data, u32 flags)
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000161{
Valentine Barshakd94bad82007-10-08 22:51:24 +1000162 resource_size_t base = cfg_addr & PAGE_MASK;
Kumar Galad5269962007-07-19 15:44:52 -0500163 void __iomem *mbase;
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000164
165 mbase = ioremap(base, PAGE_SIZE);
Kumar Galad5269962007-07-19 15:44:52 -0500166 hose->cfg_addr = mbase + (cfg_addr & ~PAGE_MASK);
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000167 if ((cfg_data & PAGE_MASK) != base)
168 mbase = ioremap(cfg_data & PAGE_MASK, PAGE_SIZE);
Kumar Galad5269962007-07-19 15:44:52 -0500169 hose->cfg_data = mbase + (cfg_data & ~PAGE_MASK);
170 hose->ops = &indirect_pci_ops;
Kumar Gala7659c032007-07-25 00:29:53 -0500171 hose->indirect_type = flags;
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000172}