blob: a1775445005a2f4cf078ab4962f3a33778b9c7e9 [file] [log] [blame]
Sergei Antonov026f1e92023-07-30 21:17:09 +03001// 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
8struct ftpci100_data {
9 void *reg_base;
10};
11
12/* AHB Control Registers */
13struct 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
21static 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(&regs->conf, PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset));
30 data = in_le32(&regs->data);
31 *valuep = pci_conv_32_to_size(data, offset, size);
32
33 return 0;
34}
35
36static 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(&regs->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(&regs->data);
50
51 data = pci_conv_size_to_32(old, value, offset, size);
52 }
53
54 out_le32(&regs->data, data);
55
56 return 0;
57}
58
59static 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
78static const struct dm_pci_ops ftpci100_ops = {
79 .read_config = ftpci100_read_config,
80 .write_config = ftpci100_write_config,
81};
82
83static const struct udevice_id ftpci100_ids[] = {
84 { .compatible = "faraday,ftpci100" },
85 { }
86};
87
88U_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};