blob: c6c8b526a4f6e5e68b569dd6451ad339d39cb78c [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
Rojhalat Ibrahim50d8f872013-04-08 10:15:28 +020023int indirect_read_config(struct pci_bus *bus, unsigned int devfn,
24 int offset, int len, u32 *val)
Paul Mackerrasdaec9622005-10-10 22:25:26 +100025{
Kumar Gala19afa402009-04-30 03:10:07 +000026 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasdaec9622005-10-10 22:25:26 +100027 volatile void __iomem *cfg_data;
28 u8 cfg_type = 0;
Kumar Galaab0f9ad2007-06-25 15:19:48 -050029 u32 bus_no, reg;
Paul Mackerrasdaec9622005-10-10 22:25:26 +100030
Kumar Gala62c66c82007-07-11 13:22:41 -050031 if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK) {
32 if (bus->number != hose->first_busno)
33 return PCIBIOS_DEVICE_NOT_FOUND;
34 if (devfn != 0)
35 return PCIBIOS_DEVICE_NOT_FOUND;
36 }
37
Paul Mackerrasdaec9622005-10-10 22:25:26 +100038 if (ppc_md.pci_exclude_device)
Kumar Gala7d52c7b2007-06-22 00:23:57 -050039 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
Paul Mackerrasdaec9622005-10-10 22:25:26 +100040 return PCIBIOS_DEVICE_NOT_FOUND;
Kumar Gala62c66c82007-07-11 13:22:41 -050041
Kumar Galaab0f9ad2007-06-25 15:19:48 -050042 if (hose->indirect_type & PPC_INDIRECT_TYPE_SET_CFG_TYPE)
Paul Mackerrasdaec9622005-10-10 22:25:26 +100043 if (bus->number != hose->first_busno)
44 cfg_type = 1;
45
Kumar Gala5ab65ec2007-06-25 13:09:42 -050046 bus_no = (bus->number == hose->first_busno) ?
Kumar Gala0a3786c2007-06-25 13:32:48 -050047 hose->self_busno : bus->number;
Kumar Gala5ab65ec2007-06-25 13:09:42 -050048
Kumar Galaab0f9ad2007-06-25 15:19:48 -050049 if (hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG)
50 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
51 else
52 reg = offset & 0xfc;
53
Kumar Gala2e56ff22007-07-19 16:07:35 -050054 if (hose->indirect_type & PPC_INDIRECT_TYPE_BIG_ENDIAN)
55 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
56 (devfn << 8) | reg | cfg_type));
57 else
58 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
59 (devfn << 8) | reg | cfg_type));
Paul Mackerrasdaec9622005-10-10 22:25:26 +100060
61 /*
62 * Note: the caller has already checked that offset is
63 * suitably aligned and that len is 1, 2 or 4.
64 */
65 cfg_data = hose->cfg_data + (offset & 3);
66 switch (len) {
67 case 1:
68 *val = in_8(cfg_data);
69 break;
70 case 2:
71 *val = in_le16(cfg_data);
72 break;
73 default:
74 *val = in_le32(cfg_data);
75 break;
76 }
77 return PCIBIOS_SUCCESSFUL;
78}
79
Rojhalat Ibrahim50d8f872013-04-08 10:15:28 +020080int indirect_write_config(struct pci_bus *bus, unsigned int devfn,
81 int offset, int len, u32 val)
Paul Mackerrasdaec9622005-10-10 22:25:26 +100082{
Kumar Gala19afa402009-04-30 03:10:07 +000083 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasdaec9622005-10-10 22:25:26 +100084 volatile void __iomem *cfg_data;
85 u8 cfg_type = 0;
Kumar Galaab0f9ad2007-06-25 15:19:48 -050086 u32 bus_no, reg;
Paul Mackerrasdaec9622005-10-10 22:25:26 +100087
Kumar Gala62c66c82007-07-11 13:22:41 -050088 if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK) {
89 if (bus->number != hose->first_busno)
90 return PCIBIOS_DEVICE_NOT_FOUND;
91 if (devfn != 0)
92 return PCIBIOS_DEVICE_NOT_FOUND;
93 }
94
Paul Mackerrasdaec9622005-10-10 22:25:26 +100095 if (ppc_md.pci_exclude_device)
Kumar Gala7d52c7b2007-06-22 00:23:57 -050096 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
Paul Mackerrasdaec9622005-10-10 22:25:26 +100097 return PCIBIOS_DEVICE_NOT_FOUND;
98
Kumar Galaab0f9ad2007-06-25 15:19:48 -050099 if (hose->indirect_type & PPC_INDIRECT_TYPE_SET_CFG_TYPE)
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000100 if (bus->number != hose->first_busno)
101 cfg_type = 1;
102
Kumar Gala5ab65ec2007-06-25 13:09:42 -0500103 bus_no = (bus->number == hose->first_busno) ?
Kumar Gala0a3786c2007-06-25 13:32:48 -0500104 hose->self_busno : bus->number;
Kumar Gala5ab65ec2007-06-25 13:09:42 -0500105
Kumar Galaab0f9ad2007-06-25 15:19:48 -0500106 if (hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG)
107 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
108 else
109 reg = offset & 0xfc;
110
Kumar Gala2e56ff22007-07-19 16:07:35 -0500111 if (hose->indirect_type & PPC_INDIRECT_TYPE_BIG_ENDIAN)
112 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
113 (devfn << 8) | reg | cfg_type));
114 else
115 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
116 (devfn << 8) | reg | cfg_type));
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000117
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300118 /* suppress setting of PCI_PRIMARY_BUS */
Kumar Gala476f5772007-06-26 12:12:55 -0500119 if (hose->indirect_type & PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS)
120 if ((offset == PCI_PRIMARY_BUS) &&
121 (bus->number == hose->first_busno))
122 val &= 0xffffff00;
123
Josh Boyer5ce4b592008-06-17 19:01:38 -0400124 /* Workaround for PCI_28 Errata in 440EPx/GRx */
125 if ((hose->indirect_type & PPC_INDIRECT_TYPE_BROKEN_MRM) &&
126 offset == PCI_CACHE_LINE_SIZE) {
127 val = 0;
128 }
129
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000130 /*
131 * Note: the caller has already checked that offset is
132 * suitably aligned and that len is 1, 2 or 4.
133 */
134 cfg_data = hose->cfg_data + (offset & 3);
135 switch (len) {
136 case 1:
137 out_8(cfg_data, val);
138 break;
139 case 2:
140 out_le16(cfg_data, val);
141 break;
142 default:
143 out_le32(cfg_data, val);
144 break;
145 }
146 return PCIBIOS_SUCCESSFUL;
147}
148
149static struct pci_ops indirect_pci_ops =
150{
Nathan Lynchc78d4532007-08-10 05:18:45 +1000151 .read = indirect_read_config,
152 .write = indirect_write_config,
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000153};
154
155void __init
Valentine Barshakd94bad82007-10-08 22:51:24 +1000156setup_indirect_pci(struct pci_controller* hose,
157 resource_size_t cfg_addr,
158 resource_size_t cfg_data, u32 flags)
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000159{
Valentine Barshakd94bad82007-10-08 22:51:24 +1000160 resource_size_t base = cfg_addr & PAGE_MASK;
Kumar Galad5269962007-07-19 15:44:52 -0500161 void __iomem *mbase;
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000162
163 mbase = ioremap(base, PAGE_SIZE);
Kumar Galad5269962007-07-19 15:44:52 -0500164 hose->cfg_addr = mbase + (cfg_addr & ~PAGE_MASK);
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000165 if ((cfg_data & PAGE_MASK) != base)
166 mbase = ioremap(cfg_data & PAGE_MASK, PAGE_SIZE);
Kumar Galad5269962007-07-19 15:44:52 -0500167 hose->cfg_data = mbase + (cfg_data & ~PAGE_MASK);
168 hose->ops = &indirect_pci_ops;
Kumar Gala7659c032007-07-25 00:29:53 -0500169 hose->indirect_type = flags;
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000170}