1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
5 * Based on the Linux implementation.
6 * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
7 * Authors: Carsten Langgaard <carstenl@mips.com>
8 * Maciej W. Rozycki <macro@mips.com>
16 #include <pci_gt64120.h>
20 #define PCI_ACCESS_READ 0
21 #define PCI_ACCESS_WRITE 1
31 struct gt64120_pci_controller {
32 struct pci_controller hose;
33 struct gt64120_regs *regs;
36 static inline struct gt64120_pci_controller *
37 hose_to_gt64120(struct pci_controller *hose)
39 return container_of(hose, struct gt64120_pci_controller, hose);
42 #define GT_INTRCAUSE_ABORT_BITS \
43 (GT_INTRCAUSE_MASABORT0_BIT | GT_INTRCAUSE_TARABORT0_BIT)
45 static int gt_config_access(struct gt64120_pci_controller *gt,
46 unsigned char access_type, pci_dev_t bdf,
49 unsigned int bus = PCI_BUS(bdf);
50 unsigned int dev = PCI_DEV(bdf);
51 unsigned int func = PCI_FUNC(bdf);
56 if (bus == 0 && dev >= 31) {
57 /* Because of a bug in the galileo (for slot 31). */
61 if (access_type == PCI_ACCESS_WRITE)
62 debug("PCI WR %02x:%02x.%x reg:%02d data:%08x\n",
63 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), where, *data);
65 /* Clear cause register bits */
66 writel(~GT_INTRCAUSE_ABORT_BITS, >->regs->intrcause);
68 addr = PCI_CONF1_ADDRESS(bus, dev, func, where);
71 writel(addr, >->regs->pci0_cfgaddr);
73 if (access_type == PCI_ACCESS_WRITE) {
74 if (bus == 0 && dev == 0) {
76 * The Galileo system controller is acting
77 * differently than other devices.
81 val = cpu_to_le32(*data);
84 writel(val, >->regs->pci0_cfgdata);
86 val = readl(>->regs->pci0_cfgdata);
88 if (bus == 0 && dev == 0) {
90 * The Galileo system controller is acting
91 * differently than other devices.
95 *data = le32_to_cpu(val);
99 /* Check for master or target abort */
100 intr = readl(>->regs->intrcause);
101 if (intr & GT_INTRCAUSE_ABORT_BITS) {
102 /* Error occurred, clear abort bits */
103 writel(~GT_INTRCAUSE_ABORT_BITS, >->regs->intrcause);
107 if (access_type == PCI_ACCESS_READ)
108 debug("PCI RD %02x:%02x.%x reg:%02d data:%08x\n",
109 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), where, *data);
114 static int gt64120_pci_read_config(const struct udevice *dev, pci_dev_t bdf,
115 uint where, ulong *val,
116 enum pci_size_t size)
118 struct gt64120_pci_controller *gt = dev_get_priv(dev);
121 if (gt_config_access(gt, PCI_ACCESS_READ, bdf, where, &data)) {
122 *val = pci_get_ff(size);
126 *val = pci_conv_32_to_size(data, where, size);
131 static int gt64120_pci_write_config(struct udevice *dev, pci_dev_t bdf,
132 uint where, ulong val,
133 enum pci_size_t size)
135 struct gt64120_pci_controller *gt = dev_get_priv(dev);
138 if (size == PCI_SIZE_32) {
143 if (gt_config_access(gt, PCI_ACCESS_READ, bdf, where, &old))
146 data = pci_conv_size_to_32(old, val, where, size);
149 gt_config_access(gt, PCI_ACCESS_WRITE, bdf, where, &data);
154 static int gt64120_pci_probe(struct udevice *dev)
156 struct gt64120_pci_controller *gt = dev_get_priv(dev);
158 gt->regs = dev_remap_addr(dev);
165 static const struct dm_pci_ops gt64120_pci_ops = {
166 .read_config = gt64120_pci_read_config,
167 .write_config = gt64120_pci_write_config,
170 static const struct udevice_id gt64120_pci_ids[] = {
171 { .compatible = "marvell,pci-gt64120" },
175 U_BOOT_DRIVER(gt64120_pci) = {
176 .name = "gt64120_pci",
178 .of_match = gt64120_pci_ids,
179 .ops = >64120_pci_ops,
180 .probe = gt64120_pci_probe,
181 .priv_auto = sizeof(struct gt64120_pci_controller),