Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Paul Burton | 234882c | 2013-11-08 11:18:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Imagination Technologies |
Paul Burton | b8551b9 | 2017-10-30 16:58:21 -0700 | [diff] [blame] | 4 | * Author: Paul Burton <paul.burton@mips.com> |
Paul Burton | 234882c | 2013-11-08 11:18:50 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Daniel Schwierzeck | cbf106c | 2021-07-15 20:53:58 +0200 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Paul Burton | 234882c | 2013-11-08 11:18:50 +0000 | [diff] [blame] | 9 | #include <msc01.h> |
| 10 | #include <pci.h> |
| 11 | #include <pci_msc01.h> |
| 12 | #include <asm/io.h> |
| 13 | |
| 14 | #define PCI_ACCESS_READ 0 |
| 15 | #define PCI_ACCESS_WRITE 1 |
| 16 | |
| 17 | struct msc01_pci_controller { |
| 18 | struct pci_controller hose; |
| 19 | void *base; |
| 20 | }; |
| 21 | |
| 22 | static inline struct msc01_pci_controller * |
| 23 | hose_to_msc01(struct pci_controller *hose) |
| 24 | { |
| 25 | return container_of(hose, struct msc01_pci_controller, hose); |
| 26 | } |
| 27 | |
| 28 | static int msc01_config_access(struct msc01_pci_controller *msc01, |
| 29 | unsigned char access_type, pci_dev_t bdf, |
| 30 | int where, u32 *data) |
| 31 | { |
| 32 | const u32 aborts = MSC01_PCI_INTSTAT_MA_MSK | MSC01_PCI_INTSTAT_TA_MSK; |
| 33 | void *intstat = msc01->base + MSC01_PCI_INTSTAT_OFS; |
| 34 | void *cfgdata = msc01->base + MSC01_PCI_CFGDATA_OFS; |
| 35 | unsigned int bus = PCI_BUS(bdf); |
| 36 | unsigned int dev = PCI_DEV(bdf); |
| 37 | unsigned int devfn = PCI_DEV(bdf) << 3 | PCI_FUNC(bdf); |
| 38 | |
| 39 | /* clear abort status */ |
| 40 | __raw_writel(aborts, intstat); |
| 41 | |
| 42 | /* setup address */ |
| 43 | __raw_writel((bus << MSC01_PCI_CFGADDR_BNUM_SHF) | |
| 44 | (dev << MSC01_PCI_CFGADDR_DNUM_SHF) | |
| 45 | (devfn << MSC01_PCI_CFGADDR_FNUM_SHF) | |
| 46 | ((where / 4) << MSC01_PCI_CFGADDR_RNUM_SHF), |
| 47 | msc01->base + MSC01_PCI_CFGADDR_OFS); |
| 48 | |
| 49 | /* perform access */ |
| 50 | if (access_type == PCI_ACCESS_WRITE) |
| 51 | __raw_writel(*data, cfgdata); |
| 52 | else |
| 53 | *data = __raw_readl(cfgdata); |
| 54 | |
| 55 | /* check for aborts */ |
| 56 | if (__raw_readl(intstat) & aborts) { |
| 57 | /* clear abort status */ |
| 58 | __raw_writel(aborts, intstat); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Daniel Schwierzeck | cbf106c | 2021-07-15 20:53:58 +0200 | [diff] [blame] | 65 | #if !IS_ENABLED(CONFIG_DM_PCI) |
Paul Burton | 234882c | 2013-11-08 11:18:50 +0000 | [diff] [blame] | 66 | static int msc01_read_config_dword(struct pci_controller *hose, pci_dev_t dev, |
| 67 | int where, u32 *value) |
| 68 | { |
| 69 | struct msc01_pci_controller *msc01 = hose_to_msc01(hose); |
| 70 | |
| 71 | *value = 0xffffffff; |
| 72 | return msc01_config_access(msc01, PCI_ACCESS_READ, dev, where, value); |
| 73 | } |
| 74 | |
| 75 | static int msc01_write_config_dword(struct pci_controller *hose, pci_dev_t dev, |
| 76 | int where, u32 value) |
| 77 | { |
| 78 | struct msc01_pci_controller *gt = hose_to_msc01(hose); |
| 79 | u32 data = value; |
| 80 | |
| 81 | return msc01_config_access(gt, PCI_ACCESS_WRITE, dev, where, &data); |
| 82 | } |
| 83 | |
| 84 | void msc01_pci_init(void *base, unsigned long sys_bus, unsigned long sys_phys, |
| 85 | unsigned long sys_size, unsigned long mem_bus, |
| 86 | unsigned long mem_phys, unsigned long mem_size, |
| 87 | unsigned long io_bus, unsigned long io_phys, |
| 88 | unsigned long io_size) |
| 89 | { |
| 90 | static struct msc01_pci_controller global_msc01; |
| 91 | struct msc01_pci_controller *msc01; |
| 92 | struct pci_controller *hose; |
| 93 | |
| 94 | msc01 = &global_msc01; |
| 95 | msc01->base = base; |
| 96 | |
| 97 | hose = &msc01->hose; |
| 98 | |
| 99 | hose->first_busno = 0; |
| 100 | hose->last_busno = 0; |
| 101 | |
| 102 | /* System memory space */ |
| 103 | pci_set_region(&hose->regions[0], sys_bus, sys_phys, sys_size, |
| 104 | PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
| 105 | |
| 106 | /* PCI memory space */ |
| 107 | pci_set_region(&hose->regions[1], mem_bus, mem_phys, mem_size, |
| 108 | PCI_REGION_MEM); |
| 109 | |
| 110 | /* PCI I/O space */ |
| 111 | pci_set_region(&hose->regions[2], io_bus, io_phys, io_size, |
| 112 | PCI_REGION_IO); |
| 113 | |
| 114 | hose->region_count = 3; |
| 115 | |
| 116 | pci_set_ops(hose, |
| 117 | pci_hose_read_config_byte_via_dword, |
| 118 | pci_hose_read_config_word_via_dword, |
| 119 | msc01_read_config_dword, |
| 120 | pci_hose_write_config_byte_via_dword, |
| 121 | pci_hose_write_config_word_via_dword, |
| 122 | msc01_write_config_dword); |
| 123 | |
| 124 | pci_register_hose(hose); |
| 125 | hose->last_busno = pci_hose_scan(hose); |
| 126 | } |
Daniel Schwierzeck | cbf106c | 2021-07-15 20:53:58 +0200 | [diff] [blame] | 127 | #else |
| 128 | static int msc01_pci_read_config(const struct udevice *dev, pci_dev_t bdf, |
| 129 | uint where, ulong *val, enum pci_size_t size) |
| 130 | { |
| 131 | struct msc01_pci_controller *msc01 = dev_get_priv(dev); |
| 132 | u32 data = 0; |
| 133 | |
| 134 | if (msc01_config_access(msc01, PCI_ACCESS_READ, bdf, where, &data)) { |
| 135 | *val = pci_get_ff(size); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | *val = pci_conv_32_to_size(data, where, size); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int msc01_pci_write_config(struct udevice *dev, pci_dev_t bdf, |
| 145 | uint where, ulong val, enum pci_size_t size) |
| 146 | { |
| 147 | struct msc01_pci_controller *msc01 = dev_get_priv(dev); |
| 148 | u32 data = 0; |
| 149 | |
| 150 | if (size == PCI_SIZE_32) { |
| 151 | data = val; |
| 152 | } else { |
| 153 | u32 old; |
| 154 | |
| 155 | if (msc01_config_access(msc01, PCI_ACCESS_READ, bdf, where, &old)) |
| 156 | return 0; |
| 157 | |
| 158 | data = pci_conv_size_to_32(old, val, where, size); |
| 159 | } |
| 160 | |
| 161 | msc01_config_access(msc01, PCI_ACCESS_WRITE, bdf, where, &data); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int msc01_pci_probe(struct udevice *dev) |
| 167 | { |
| 168 | struct msc01_pci_controller *msc01 = dev_get_priv(dev); |
| 169 | |
| 170 | msc01->base = dev_remap_addr(dev); |
| 171 | if (!msc01->base) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static const struct dm_pci_ops msc01_pci_ops = { |
| 178 | .read_config = msc01_pci_read_config, |
| 179 | .write_config = msc01_pci_write_config, |
| 180 | }; |
| 181 | |
| 182 | static const struct udevice_id msc01_pci_ids[] = { |
| 183 | { .compatible = "mips,pci-msc01" }, |
| 184 | { } |
| 185 | }; |
| 186 | |
| 187 | U_BOOT_DRIVER(msc01_pci) = { |
| 188 | .name = "msc01_pci", |
| 189 | .id = UCLASS_PCI, |
| 190 | .of_match = msc01_pci_ids, |
| 191 | .ops = &msc01_pci_ops, |
| 192 | .probe = msc01_pci_probe, |
| 193 | .priv_auto = sizeof(struct msc01_pci_controller), |
| 194 | }; |
| 195 | #endif |