1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Marvell International Ltd.
5 * https://spdx.org/licenses
14 #include <asm/global_data.h>
18 #include <linux/ioport.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 * This driver supports multiple types of operations / host bridges / busses:
25 * OTX_ECAM: Octeon TX & TX2 ECAM (Enhanced Configuration Access Mechanism)
26 * Used to access the internal on-chip devices which are connected
28 * OTX_PEM: Octeon TX PEM (PCI Express MAC)
29 * Used to access the external (off-chip) PCI devices
30 * OTX2_PEM: Octeon TX2 PEM (PCI Express MAC)
31 * Used to access the external (off-chip) PCI devices
40 * struct octeontx_pci - Driver private data
41 * @type: Device type matched via compatible (e.g. OTX_ECAM etc)
42 * @cfg: Config resource
52 static ulong readl_size(uintptr_t addr, enum pci_size_t size)
67 printf("Invalid size\n");
74 static void writel_size(uintptr_t addr, enum pci_size_t size, ulong valuep)
87 printf("Invalid size\n");
91 static bool octeontx_bdf_invalid(pci_dev_t bdf)
93 if (PCI_BUS(bdf) == 1 && PCI_DEV(bdf) > 0)
99 static int octeontx_ecam_read_config(const struct udevice *bus, pci_dev_t bdf,
100 uint offset, ulong *valuep,
101 enum pci_size_t size)
103 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
104 struct pci_controller *hose = dev_get_uclass_priv(bus);
107 address = PCIE_ECAM_OFFSET(PCI_BUS(bdf) + pcie->bus.start - hose->first_busno,
108 PCI_DEV(bdf), PCI_FUNC(bdf), offset);
109 *valuep = readl_size(pcie->cfg.start + address, size);
111 debug("%02x.%02x.%02x: u%d %x -> %lx\n",
112 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), size, offset, *valuep);
117 static int octeontx_ecam_write_config(struct udevice *bus, pci_dev_t bdf,
118 uint offset, ulong value,
119 enum pci_size_t size)
121 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
122 struct pci_controller *hose = dev_get_uclass_priv(bus);
125 address = PCIE_ECAM_OFFSET(PCI_BUS(bdf) + pcie->bus.start - hose->first_busno,
126 PCI_DEV(bdf), PCI_FUNC(bdf), offset);
127 writel_size(pcie->cfg.start + address, size, value);
129 debug("%02x.%02x.%02x: u%d %x <- %lx\n",
130 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), size, offset, value);
135 static int octeontx_pem_read_config(const struct udevice *bus, pci_dev_t bdf,
136 uint offset, ulong *valuep,
137 enum pci_size_t size)
139 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
140 struct pci_controller *hose = dev_get_uclass_priv(bus);
143 u8 pri_bus = pcie->bus.start + 1 - hose->first_busno;
144 u32 bus_offs = (pri_bus << 16) | (pri_bus << 8) | (pri_bus << 0);
146 *valuep = pci_conv_32_to_size(~0UL, offset, size);
148 if (octeontx_bdf_invalid(bdf))
151 address = PCIE_ECAM_OFFSET(PCI_BUS(bdf) + 1 - hose->first_busno,
152 PCI_DEV(bdf), PCI_FUNC(bdf), 0) << 4;
153 *valuep = readl_size(pcie->cfg.start + address + offset, size);
155 hdrtype = readb(pcie->cfg.start + address + PCI_HEADER_TYPE);
156 if (hdrtype == PCI_HEADER_TYPE_BRIDGE &&
157 offset >= PCI_PRIMARY_BUS &&
158 offset <= PCI_SUBORDINATE_BUS &&
159 *valuep != pci_conv_32_to_size(~0UL, offset, size))
160 *valuep -= pci_conv_32_to_size(bus_offs, offset, size);
165 static int octeontx_pem_write_config(struct udevice *bus, pci_dev_t bdf,
166 uint offset, ulong value,
167 enum pci_size_t size)
169 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
170 struct pci_controller *hose = dev_get_uclass_priv(bus);
173 u8 pri_bus = pcie->bus.start + 1 - hose->first_busno;
174 u32 bus_offs = (pri_bus << 16) | (pri_bus << 8) | (pri_bus << 0);
176 address = PCIE_ECAM_OFFSET(PCI_BUS(bdf) + 1 - hose->first_busno,
177 PCI_DEV(bdf), PCI_FUNC(bdf), 0) << 4;
179 hdrtype = readb(pcie->cfg.start + address + PCI_HEADER_TYPE);
180 if (hdrtype == PCI_HEADER_TYPE_BRIDGE &&
181 offset >= PCI_PRIMARY_BUS &&
182 offset <= PCI_SUBORDINATE_BUS &&
183 value != pci_conv_32_to_size(~0UL, offset, size))
184 value += pci_conv_32_to_size(bus_offs, offset, size);
186 if (octeontx_bdf_invalid(bdf))
189 writel_size(pcie->cfg.start + address + offset, size, value);
191 debug("%02x.%02x.%02x: u%d %x (%lx) <- %lx\n",
192 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), size, offset,
198 static int octeontx2_pem_read_config(const struct udevice *bus, pci_dev_t bdf,
199 uint offset, ulong *valuep,
200 enum pci_size_t size)
202 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
203 struct pci_controller *hose = dev_get_uclass_priv(bus);
206 *valuep = pci_conv_32_to_size(~0UL, offset, size);
208 if (octeontx_bdf_invalid(bdf))
211 address = PCIE_ECAM_OFFSET(PCI_BUS(bdf) + 1 - hose->first_busno,
212 PCI_DEV(bdf), PCI_FUNC(bdf), offset);
213 *valuep = readl_size(pcie->cfg.start + address, size);
215 debug("%02x.%02x.%02x: u%d %x (%lx) -> %lx\n",
216 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), size, offset,
222 static int octeontx2_pem_write_config(struct udevice *bus, pci_dev_t bdf,
223 uint offset, ulong value,
224 enum pci_size_t size)
226 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
227 struct pci_controller *hose = dev_get_uclass_priv(bus);
230 if (octeontx_bdf_invalid(bdf))
233 address = PCIE_ECAM_OFFSET(PCI_BUS(bdf) + 1 - hose->first_busno,
234 PCI_DEV(bdf), PCI_FUNC(bdf), offset);
235 writel_size(pcie->cfg.start + address, size, value);
237 debug("%02x.%02x.%02x: u%d %x (%lx) <- %lx\n",
238 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), size, offset,
244 int pci_octeontx_read_config(const struct udevice *bus, pci_dev_t bdf,
245 uint offset, ulong *valuep,
246 enum pci_size_t size)
248 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
251 switch (pcie->type) {
253 ret = octeontx_ecam_read_config(bus, bdf, offset, valuep,
257 ret = octeontx_pem_read_config(bus, bdf, offset, valuep,
261 ret = octeontx2_pem_read_config(bus, bdf, offset, valuep,
269 int pci_octeontx_write_config(struct udevice *bus, pci_dev_t bdf,
270 uint offset, ulong value,
271 enum pci_size_t size)
273 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(bus);
276 switch (pcie->type) {
278 ret = octeontx_ecam_write_config(bus, bdf, offset, value,
282 ret = octeontx_pem_write_config(bus, bdf, offset, value,
286 ret = octeontx2_pem_write_config(bus, bdf, offset, value,
294 static int pci_octeontx_of_to_plat(struct udevice *dev)
299 static int pci_octeontx_probe(struct udevice *dev)
301 struct octeontx_pci *pcie = (struct octeontx_pci *)dev_get_priv(dev);
304 pcie->type = dev_get_driver_data(dev);
306 err = dev_read_resource(dev, 0, &pcie->cfg);
308 debug("Error reading resource: %s\n", fdt_strerror(err));
312 err = dev_read_pci_bus_range(dev, &pcie->bus);
314 debug("Error reading resource: %s\n", fdt_strerror(err));
321 static const struct dm_pci_ops pci_octeontx_ops = {
322 .read_config = pci_octeontx_read_config,
323 .write_config = pci_octeontx_write_config,
326 static const struct udevice_id pci_octeontx_ids[] = {
327 { .compatible = "cavium,pci-host-thunder-ecam", .data = OTX_ECAM },
328 { .compatible = "cavium,pci-host-octeontx-ecam", .data = OTX_ECAM },
329 { .compatible = "pci-host-ecam-generic", .data = OTX_ECAM },
330 { .compatible = "cavium,pci-host-thunder-pem", .data = OTX_PEM },
331 { .compatible = "marvell,pci-host-octeontx2-pem", .data = OTX2_PEM },
335 U_BOOT_DRIVER(pci_octeontx) = {
336 .name = "pci_octeontx",
338 .of_match = pci_octeontx_ids,
339 .ops = &pci_octeontx_ops,
340 .of_to_plat = pci_octeontx_of_to_plat,
341 .probe = pci_octeontx_probe,
342 .priv_auto = sizeof(struct octeontx_pci),
343 .flags = DM_FLAG_PRE_RELOC,