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