Loading...
1/*
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 <linux/io.h>
19#include <asm/prom.h>
20#include <asm/pci-bridge.h>
21
22static int
23indirect_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
24 int len, u32 *val)
25{
26 struct pci_controller *hose = pci_bus_to_host(bus);
27 volatile void __iomem *cfg_data;
28 u8 cfg_type = 0;
29 u32 bus_no, reg;
30
31 if (hose->indirect_type & 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
38 if (hose->indirect_type & INDIRECT_TYPE_SET_CFG_TYPE)
39 if (bus->number != hose->first_busno)
40 cfg_type = 1;
41
42 bus_no = (bus->number == hose->first_busno) ?
43 hose->self_busno : bus->number;
44
45 if (hose->indirect_type & INDIRECT_TYPE_EXT_REG)
46 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
47 else
48 reg = offset & 0xfc; /* Only 3 bits for function */
49
50 if (hose->indirect_type & INDIRECT_TYPE_BIG_ENDIAN)
51 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
52 (devfn << 8) | reg | cfg_type));
53 else
54 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
55 (devfn << 8) | reg | cfg_type));
56
57 /*
58 * Note: the caller has already checked that offset is
59 * suitably aligned and that len is 1, 2 or 4.
60 */
61 cfg_data = hose->cfg_data + (offset & 3); /* Only 3 bits for function */
62 switch (len) {
63 case 1:
64 *val = in_8(cfg_data);
65 break;
66 case 2:
67 *val = in_le16(cfg_data);
68 break;
69 default:
70 *val = in_le32(cfg_data);
71 break;
72 }
73 return PCIBIOS_SUCCESSFUL;
74}
75
76static int
77indirect_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
78 int len, u32 val)
79{
80 struct pci_controller *hose = pci_bus_to_host(bus);
81 volatile void __iomem *cfg_data;
82 u8 cfg_type = 0;
83 u32 bus_no, reg;
84
85 if (hose->indirect_type & INDIRECT_TYPE_NO_PCIE_LINK) {
86 if (bus->number != hose->first_busno)
87 return PCIBIOS_DEVICE_NOT_FOUND;
88 if (devfn != 0)
89 return PCIBIOS_DEVICE_NOT_FOUND;
90 }
91
92 if (hose->indirect_type & INDIRECT_TYPE_SET_CFG_TYPE)
93 if (bus->number != hose->first_busno)
94 cfg_type = 1;
95
96 bus_no = (bus->number == hose->first_busno) ?
97 hose->self_busno : bus->number;
98
99 if (hose->indirect_type & INDIRECT_TYPE_EXT_REG)
100 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
101 else
102 reg = offset & 0xfc;
103
104 if (hose->indirect_type & INDIRECT_TYPE_BIG_ENDIAN)
105 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
106 (devfn << 8) | reg | cfg_type));
107 else
108 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
109 (devfn << 8) | reg | cfg_type));
110
111 /* suppress setting of PCI_PRIMARY_BUS */
112 if (hose->indirect_type & INDIRECT_TYPE_SURPRESS_PRIMARY_BUS)
113 if ((offset == PCI_PRIMARY_BUS) &&
114 (bus->number == hose->first_busno))
115 val &= 0xffffff00;
116
117 /* Workaround for PCI_28 Errata in 440EPx/GRx */
118 if ((hose->indirect_type & INDIRECT_TYPE_BROKEN_MRM) &&
119 offset == PCI_CACHE_LINE_SIZE) {
120 val = 0;
121 }
122
123 /*
124 * Note: the caller has already checked that offset is
125 * suitably aligned and that len is 1, 2 or 4.
126 */
127 cfg_data = hose->cfg_data + (offset & 3);
128 switch (len) {
129 case 1:
130 out_8(cfg_data, val);
131 break;
132 case 2:
133 out_le16(cfg_data, val);
134 break;
135 default:
136 out_le32(cfg_data, val);
137 break;
138 }
139
140 return PCIBIOS_SUCCESSFUL;
141}
142
143static struct pci_ops indirect_pci_ops = {
144 .read = indirect_read_config,
145 .write = indirect_write_config,
146};
147
148void __init
149setup_indirect_pci(struct pci_controller *hose,
150 resource_size_t cfg_addr,
151 resource_size_t cfg_data, u32 flags)
152{
153 resource_size_t base = cfg_addr & PAGE_MASK;
154 void __iomem *mbase;
155
156 mbase = ioremap(base, PAGE_SIZE);
157 hose->cfg_addr = mbase + (cfg_addr & ~PAGE_MASK);
158 if ((cfg_data & PAGE_MASK) != base)
159 mbase = ioremap(cfg_data & PAGE_MASK, PAGE_SIZE);
160 hose->cfg_data = mbase + (cfg_data & ~PAGE_MASK);
161 hose->ops = &indirect_pci_ops;
162 hose->indirect_type = flags;
163}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Support for indirect PCI bridges.
4 *
5 * Copyright (C) 1998 Gabriel Paubert.
6 */
7
8#include <linux/kernel.h>
9#include <linux/pci.h>
10#include <linux/delay.h>
11#include <linux/string.h>
12#include <linux/init.h>
13
14#include <linux/io.h>
15#include <asm/pci-bridge.h>
16
17static int
18indirect_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
19 int len, u32 *val)
20{
21 struct pci_controller *hose = pci_bus_to_host(bus);
22 volatile void __iomem *cfg_data;
23 u8 cfg_type = 0;
24 u32 bus_no, reg;
25
26 if (hose->indirect_type & INDIRECT_TYPE_NO_PCIE_LINK) {
27 if (bus->number != hose->first_busno)
28 return PCIBIOS_DEVICE_NOT_FOUND;
29 if (devfn != 0)
30 return PCIBIOS_DEVICE_NOT_FOUND;
31 }
32
33 if (hose->indirect_type & INDIRECT_TYPE_SET_CFG_TYPE)
34 if (bus->number != hose->first_busno)
35 cfg_type = 1;
36
37 bus_no = (bus->number == hose->first_busno) ?
38 hose->self_busno : bus->number;
39
40 if (hose->indirect_type & INDIRECT_TYPE_EXT_REG)
41 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
42 else
43 reg = offset & 0xfc; /* Only 3 bits for function */
44
45 if (hose->indirect_type & INDIRECT_TYPE_BIG_ENDIAN)
46 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
47 (devfn << 8) | reg | cfg_type));
48 else
49 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
50 (devfn << 8) | reg | cfg_type));
51
52 /*
53 * Note: the caller has already checked that offset is
54 * suitably aligned and that len is 1, 2 or 4.
55 */
56 cfg_data = hose->cfg_data + (offset & 3); /* Only 3 bits for function */
57 switch (len) {
58 case 1:
59 *val = in_8(cfg_data);
60 break;
61 case 2:
62 *val = in_le16(cfg_data);
63 break;
64 default:
65 *val = in_le32(cfg_data);
66 break;
67 }
68 return PCIBIOS_SUCCESSFUL;
69}
70
71static int
72indirect_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
73 int len, u32 val)
74{
75 struct pci_controller *hose = pci_bus_to_host(bus);
76 volatile void __iomem *cfg_data;
77 u8 cfg_type = 0;
78 u32 bus_no, reg;
79
80 if (hose->indirect_type & INDIRECT_TYPE_NO_PCIE_LINK) {
81 if (bus->number != hose->first_busno)
82 return PCIBIOS_DEVICE_NOT_FOUND;
83 if (devfn != 0)
84 return PCIBIOS_DEVICE_NOT_FOUND;
85 }
86
87 if (hose->indirect_type & INDIRECT_TYPE_SET_CFG_TYPE)
88 if (bus->number != hose->first_busno)
89 cfg_type = 1;
90
91 bus_no = (bus->number == hose->first_busno) ?
92 hose->self_busno : bus->number;
93
94 if (hose->indirect_type & INDIRECT_TYPE_EXT_REG)
95 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
96 else
97 reg = offset & 0xfc;
98
99 if (hose->indirect_type & INDIRECT_TYPE_BIG_ENDIAN)
100 out_be32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
101 (devfn << 8) | reg | cfg_type));
102 else
103 out_le32(hose->cfg_addr, (0x80000000 | (bus_no << 16) |
104 (devfn << 8) | reg | cfg_type));
105
106 /* suppress setting of PCI_PRIMARY_BUS */
107 if (hose->indirect_type & INDIRECT_TYPE_SURPRESS_PRIMARY_BUS)
108 if ((offset == PCI_PRIMARY_BUS) &&
109 (bus->number == hose->first_busno))
110 val &= 0xffffff00;
111
112 /* Workaround for PCI_28 Errata in 440EPx/GRx */
113 if ((hose->indirect_type & INDIRECT_TYPE_BROKEN_MRM) &&
114 offset == PCI_CACHE_LINE_SIZE) {
115 val = 0;
116 }
117
118 /*
119 * Note: the caller has already checked that offset is
120 * suitably aligned and that len is 1, 2 or 4.
121 */
122 cfg_data = hose->cfg_data + (offset & 3);
123 switch (len) {
124 case 1:
125 out_8(cfg_data, val);
126 break;
127 case 2:
128 out_le16(cfg_data, val);
129 break;
130 default:
131 out_le32(cfg_data, val);
132 break;
133 }
134
135 return PCIBIOS_SUCCESSFUL;
136}
137
138static struct pci_ops indirect_pci_ops = {
139 .read = indirect_read_config,
140 .write = indirect_write_config,
141};
142
143void __init
144setup_indirect_pci(struct pci_controller *hose,
145 resource_size_t cfg_addr,
146 resource_size_t cfg_data, u32 flags)
147{
148 resource_size_t base = cfg_addr & PAGE_MASK;
149 void __iomem *mbase;
150
151 mbase = ioremap(base, PAGE_SIZE);
152 hose->cfg_addr = mbase + (cfg_addr & ~PAGE_MASK);
153 if ((cfg_data & PAGE_MASK) != base)
154 mbase = ioremap(cfg_data & PAGE_MASK, PAGE_SIZE);
155 hose->cfg_data = mbase + (cfg_data & ~PAGE_MASK);
156 hose->ops = &indirect_pci_ops;
157 hose->indirect_type = flags;
158}