Sergei Antonov | 852467d | 2023-07-30 21:17:09 +0300 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | |
| 3 | #include <common.h> |
| 4 | #include <pci.h> |
| 5 | #include <dm.h> |
| 6 | #include <asm/io.h> |
| 7 | |
| 8 | struct ftpci100_data { |
| 9 | void *reg_base; |
| 10 | }; |
| 11 | |
| 12 | /* AHB Control Registers */ |
| 13 | struct ftpci100_ahbc { |
| 14 | u32 iosize; /* 0x00 - I/O Space Size Signal */ |
| 15 | u32 prot; /* 0x04 - AHB Protection */ |
| 16 | u32 rsved[8]; /* 0x08-0x24 - Reserved */ |
| 17 | u32 conf; /* 0x28 - PCI Configuration */ |
| 18 | u32 data; /* 0x2c - PCI Configuration DATA */ |
| 19 | }; |
| 20 | |
| 21 | static int ftpci100_read_config(const struct udevice *dev, pci_dev_t bdf, |
| 22 | uint offset, ulong *valuep, |
| 23 | enum pci_size_t size) |
| 24 | { |
| 25 | struct ftpci100_data *priv = dev_get_priv(dev); |
| 26 | struct ftpci100_ahbc *regs = priv->reg_base; |
| 27 | u32 data; |
| 28 | |
| 29 | out_le32(®s->conf, PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset)); |
| 30 | data = in_le32(®s->data); |
| 31 | *valuep = pci_conv_32_to_size(data, offset, size); |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int ftpci100_write_config(struct udevice *dev, pci_dev_t bdf, |
| 37 | uint offset, ulong value, |
| 38 | enum pci_size_t size) |
| 39 | { |
| 40 | struct ftpci100_data *priv = dev_get_priv(dev); |
| 41 | struct ftpci100_ahbc *regs = priv->reg_base; |
| 42 | u32 data; |
| 43 | |
| 44 | out_le32(®s->conf, PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset)); |
| 45 | |
| 46 | if (size == PCI_SIZE_32) { |
| 47 | data = value; |
| 48 | } else { |
| 49 | u32 old = in_le32(®s->data); |
| 50 | |
| 51 | data = pci_conv_size_to_32(old, value, offset, size); |
| 52 | } |
| 53 | |
| 54 | out_le32(®s->data, data); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int ftpci100_probe(struct udevice *dev) |
| 60 | { |
| 61 | struct ftpci100_data *priv = dev_get_priv(dev); |
| 62 | struct pci_region *io, *mem; |
| 63 | int count; |
| 64 | |
| 65 | count = pci_get_regions(dev, &io, &mem, NULL); |
| 66 | if (count != 2) { |
| 67 | printf("%s: wrong count of regions: %d != 2\n", dev->name, count); |
| 68 | return -EINVAL; |
| 69 | } |
| 70 | |
| 71 | priv->reg_base = phys_to_virt(io->phys_start); |
| 72 | if (!priv->reg_base) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static const struct dm_pci_ops ftpci100_ops = { |
| 79 | .read_config = ftpci100_read_config, |
| 80 | .write_config = ftpci100_write_config, |
| 81 | }; |
| 82 | |
| 83 | static const struct udevice_id ftpci100_ids[] = { |
| 84 | { .compatible = "faraday,ftpci100" }, |
| 85 | { } |
| 86 | }; |
| 87 | |
| 88 | U_BOOT_DRIVER(ftpci100_pci) = { |
| 89 | .name = "ftpci100_pci", |
| 90 | .id = UCLASS_PCI, |
| 91 | .of_match = ftpci100_ids, |
| 92 | .ops = &ftpci100_ops, |
| 93 | .probe = ftpci100_probe, |
| 94 | .priv_auto = sizeof(struct ftpci100_data), |
| 95 | }; |