Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Gabor Juhos | 652ccee | 2013-05-22 03:57:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org> |
| 4 | * |
| 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> |
Gabor Juhos | 652ccee | 2013-05-22 03:57:42 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Daniel Schwierzeck | fa3cb19 | 2021-07-15 20:53:57 +0200 | [diff] [blame^] | 11 | #include <dm.h> |
Gabor Juhos | 652ccee | 2013-05-22 03:57:42 +0000 | [diff] [blame] | 12 | #include <gt64120.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 13 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Gabor Juhos | 652ccee | 2013-05-22 03:57:42 +0000 | [diff] [blame] | 15 | #include <pci.h> |
| 16 | #include <pci_gt64120.h> |
| 17 | |
| 18 | #include <asm/io.h> |
| 19 | |
| 20 | #define PCI_ACCESS_READ 0 |
| 21 | #define PCI_ACCESS_WRITE 1 |
| 22 | |
| 23 | struct gt64120_regs { |
| 24 | u8 unused_000[0xc18]; |
| 25 | u32 intrcause; |
| 26 | u8 unused_c1c[0x0dc]; |
| 27 | u32 pci0_cfgaddr; |
| 28 | u32 pci0_cfgdata; |
| 29 | }; |
| 30 | |
| 31 | struct gt64120_pci_controller { |
| 32 | struct pci_controller hose; |
| 33 | struct gt64120_regs *regs; |
| 34 | }; |
| 35 | |
| 36 | static inline struct gt64120_pci_controller * |
| 37 | hose_to_gt64120(struct pci_controller *hose) |
| 38 | { |
| 39 | return container_of(hose, struct gt64120_pci_controller, hose); |
| 40 | } |
| 41 | |
| 42 | #define GT_INTRCAUSE_ABORT_BITS \ |
| 43 | (GT_INTRCAUSE_MASABORT0_BIT | GT_INTRCAUSE_TARABORT0_BIT) |
| 44 | |
| 45 | static int gt_config_access(struct gt64120_pci_controller *gt, |
| 46 | unsigned char access_type, pci_dev_t bdf, |
| 47 | int where, u32 *data) |
| 48 | { |
| 49 | unsigned int bus = PCI_BUS(bdf); |
| 50 | unsigned int dev = PCI_DEV(bdf); |
| 51 | unsigned int devfn = PCI_DEV(bdf) << 3 | PCI_FUNC(bdf); |
| 52 | u32 intr; |
| 53 | u32 addr; |
| 54 | u32 val; |
| 55 | |
| 56 | if (bus == 0 && dev >= 31) { |
| 57 | /* Because of a bug in the galileo (for slot 31). */ |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 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); |
| 64 | |
| 65 | /* Clear cause register bits */ |
| 66 | writel(~GT_INTRCAUSE_ABORT_BITS, >->regs->intrcause); |
| 67 | |
| 68 | addr = GT_PCI0_CFGADDR_CONFIGEN_BIT; |
| 69 | addr |= bus << GT_PCI0_CFGADDR_BUSNUM_SHF; |
| 70 | addr |= devfn << GT_PCI0_CFGADDR_FUNCTNUM_SHF; |
| 71 | addr |= (where / 4) << GT_PCI0_CFGADDR_REGNUM_SHF; |
| 72 | |
| 73 | /* Setup address */ |
| 74 | writel(addr, >->regs->pci0_cfgaddr); |
| 75 | |
| 76 | if (access_type == PCI_ACCESS_WRITE) { |
| 77 | if (bus == 0 && dev == 0) { |
| 78 | /* |
| 79 | * The Galileo system controller is acting |
| 80 | * differently than other devices. |
| 81 | */ |
| 82 | val = *data; |
| 83 | } else { |
| 84 | val = cpu_to_le32(*data); |
| 85 | } |
| 86 | |
| 87 | writel(val, >->regs->pci0_cfgdata); |
| 88 | } else { |
| 89 | val = readl(>->regs->pci0_cfgdata); |
| 90 | |
| 91 | if (bus == 0 && dev == 0) { |
| 92 | /* |
| 93 | * The Galileo system controller is acting |
| 94 | * differently than other devices. |
| 95 | */ |
| 96 | *data = val; |
| 97 | } else { |
| 98 | *data = le32_to_cpu(val); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* Check for master or target abort */ |
| 103 | intr = readl(>->regs->intrcause); |
| 104 | if (intr & GT_INTRCAUSE_ABORT_BITS) { |
| 105 | /* Error occurred, clear abort bits */ |
| 106 | writel(~GT_INTRCAUSE_ABORT_BITS, >->regs->intrcause); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | if (access_type == PCI_ACCESS_READ) |
| 111 | debug("PCI RD %02x:%02x.%x reg:%02d data:%08x\n", |
| 112 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), where, *data); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Daniel Schwierzeck | fa3cb19 | 2021-07-15 20:53:57 +0200 | [diff] [blame^] | 117 | #if !IS_ENABLED(CONFIG_DM_PCI) |
Gabor Juhos | 652ccee | 2013-05-22 03:57:42 +0000 | [diff] [blame] | 118 | static int gt_read_config_dword(struct pci_controller *hose, pci_dev_t dev, |
| 119 | int where, u32 *value) |
| 120 | { |
| 121 | struct gt64120_pci_controller *gt = hose_to_gt64120(hose); |
| 122 | |
| 123 | *value = 0xffffffff; |
| 124 | return gt_config_access(gt, PCI_ACCESS_READ, dev, where, value); |
| 125 | } |
| 126 | |
| 127 | static int gt_write_config_dword(struct pci_controller *hose, pci_dev_t dev, |
| 128 | int where, u32 value) |
| 129 | { |
| 130 | struct gt64120_pci_controller *gt = hose_to_gt64120(hose); |
| 131 | u32 data = value; |
| 132 | |
| 133 | return gt_config_access(gt, PCI_ACCESS_WRITE, dev, where, &data); |
| 134 | } |
| 135 | |
| 136 | void gt64120_pci_init(void *regs, unsigned long sys_bus, unsigned long sys_phys, |
| 137 | unsigned long sys_size, unsigned long mem_bus, |
| 138 | unsigned long mem_phys, unsigned long mem_size, |
| 139 | unsigned long io_bus, unsigned long io_phys, |
| 140 | unsigned long io_size) |
| 141 | { |
| 142 | static struct gt64120_pci_controller global_gt; |
| 143 | struct gt64120_pci_controller *gt; |
| 144 | struct pci_controller *hose; |
| 145 | |
| 146 | gt = &global_gt; |
| 147 | gt->regs = regs; |
| 148 | |
| 149 | hose = >->hose; |
| 150 | |
| 151 | hose->first_busno = 0; |
| 152 | hose->last_busno = 0; |
| 153 | |
| 154 | /* System memory space */ |
| 155 | pci_set_region(&hose->regions[0], sys_bus, sys_phys, sys_size, |
| 156 | PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
| 157 | |
| 158 | /* PCI memory space */ |
| 159 | pci_set_region(&hose->regions[1], mem_bus, mem_phys, mem_size, |
| 160 | PCI_REGION_MEM); |
| 161 | |
| 162 | /* PCI I/O space */ |
| 163 | pci_set_region(&hose->regions[2], io_bus, io_phys, io_size, |
| 164 | PCI_REGION_IO); |
| 165 | |
| 166 | hose->region_count = 3; |
| 167 | |
| 168 | pci_set_ops(hose, |
| 169 | pci_hose_read_config_byte_via_dword, |
| 170 | pci_hose_read_config_word_via_dword, |
| 171 | gt_read_config_dword, |
| 172 | pci_hose_write_config_byte_via_dword, |
| 173 | pci_hose_write_config_word_via_dword, |
| 174 | gt_write_config_dword); |
| 175 | |
| 176 | pci_register_hose(hose); |
| 177 | hose->last_busno = pci_hose_scan(hose); |
| 178 | } |
Daniel Schwierzeck | fa3cb19 | 2021-07-15 20:53:57 +0200 | [diff] [blame^] | 179 | #else |
| 180 | static int gt64120_pci_read_config(const struct udevice *dev, pci_dev_t bdf, |
| 181 | uint where, ulong *val, |
| 182 | enum pci_size_t size) |
| 183 | { |
| 184 | struct gt64120_pci_controller *gt = dev_get_priv(dev); |
| 185 | u32 data = 0; |
| 186 | |
| 187 | if (gt_config_access(gt, PCI_ACCESS_READ, bdf, where, &data)) { |
| 188 | *val = pci_get_ff(size); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | *val = pci_conv_32_to_size(data, where, size); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int gt64120_pci_write_config(struct udevice *dev, pci_dev_t bdf, |
| 198 | uint where, ulong val, |
| 199 | enum pci_size_t size) |
| 200 | { |
| 201 | struct gt64120_pci_controller *gt = dev_get_priv(dev); |
| 202 | u32 data = 0; |
| 203 | |
| 204 | if (size == PCI_SIZE_32) { |
| 205 | data = val; |
| 206 | } else { |
| 207 | u32 old; |
| 208 | |
| 209 | if (gt_config_access(gt, PCI_ACCESS_READ, bdf, where, &old)) |
| 210 | return 0; |
| 211 | |
| 212 | data = pci_conv_size_to_32(old, val, where, size); |
| 213 | } |
| 214 | |
| 215 | gt_config_access(gt, PCI_ACCESS_WRITE, bdf, where, &data); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int gt64120_pci_probe(struct udevice *dev) |
| 221 | { |
| 222 | struct gt64120_pci_controller *gt = dev_get_priv(dev); |
| 223 | |
| 224 | gt->regs = dev_remap_addr(dev); |
| 225 | if (!gt->regs) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static const struct dm_pci_ops gt64120_pci_ops = { |
| 232 | .read_config = gt64120_pci_read_config, |
| 233 | .write_config = gt64120_pci_write_config, |
| 234 | }; |
| 235 | |
| 236 | static const struct udevice_id gt64120_pci_ids[] = { |
| 237 | { .compatible = "marvell,pci-gt64120" }, |
| 238 | { } |
| 239 | }; |
| 240 | |
| 241 | U_BOOT_DRIVER(gt64120_pci) = { |
| 242 | .name = "gt64120_pci", |
| 243 | .id = UCLASS_PCI, |
| 244 | .of_match = gt64120_pci_ids, |
| 245 | .ops = >64120_pci_ops, |
| 246 | .probe = gt64120_pci_probe, |
| 247 | .priv_auto = sizeof(struct gt64120_pci_controller), |
| 248 | }; |
| 249 | #endif |