1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic PCIE host provided by e.g. QEMU
4  *
5  * Heavily based on drivers/pci/pcie_xilinx.c
6  *
7  * Copyright (C) 2016 Imagination Technologies
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <pci.h>
13 #include <asm/global_data.h>
14 
15 #include <asm/io.h>
16 
17 #define TYPE_PCI 0x1
18 
19 /**
20  * struct generic_ecam_pcie - generic_ecam PCIe controller state
21  * @cfg_base: The base address of memory mapped configuration space
22  */
23 struct generic_ecam_pcie {
24 	void *cfg_base;
25 	pci_size_t size;
26 	int first_busno;
27 };
28 
29 /**
30  * pci_generic_ecam_conf_address() - Calculate the address of a config access
31  * @bus: Pointer to the PCI bus
32  * @bdf: Identifies the PCIe device to access
33  * @offset: The offset into the device's configuration space
34  * @paddress: Pointer to the pointer to write the calculates address to
35  *
36  * Calculates the address that should be accessed to perform a PCIe
37  * configuration space access for a given device identified by the PCIe
38  * controller device @pcie and the bus, device & function numbers in @bdf. If
39  * access to the device is not valid then the function will return an error
40  * code. Otherwise the address to access will be written to the pointer pointed
41  * to by @paddress.
42  */
pci_generic_ecam_conf_address(const struct udevice * bus,pci_dev_t bdf,uint offset,void ** paddress)43 static int pci_generic_ecam_conf_address(const struct udevice *bus,
44 					 pci_dev_t bdf, uint offset,
45 					 void **paddress)
46 {
47 	struct generic_ecam_pcie *pcie = dev_get_priv(bus);
48 	void *addr;
49 
50 	addr = pcie->cfg_base;
51 
52 	if (dev_get_driver_data(bus) == TYPE_PCI) {
53 		addr += ((PCI_BUS(bdf) - pcie->first_busno) << 16) |
54 			 (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) | offset;
55 	} else {
56 		addr += PCIE_ECAM_OFFSET(PCI_BUS(bdf) - pcie->first_busno,
57 					 PCI_DEV(bdf), PCI_FUNC(bdf), offset);
58 	}
59 	*paddress = addr;
60 
61 	return 0;
62 }
63 
pci_generic_ecam_addr_valid(const struct udevice * bus,pci_dev_t bdf)64 static bool pci_generic_ecam_addr_valid(const struct udevice *bus,
65 					pci_dev_t bdf)
66 {
67 	struct generic_ecam_pcie *pcie = dev_get_priv(bus);
68 	int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16);
69 
70 	return (PCI_BUS(bdf) >= pcie->first_busno &&
71 		PCI_BUS(bdf) < pcie->first_busno + num_buses);
72 }
73 
74 /**
75  * pci_generic_ecam_read_config() - Read from configuration space
76  * @bus: Pointer to the PCI bus
77  * @bdf: Identifies the PCIe device to access
78  * @offset: The offset into the device's configuration space
79  * @valuep: A pointer at which to store the read value
80  * @size: Indicates the size of access to perform
81  *
82  * Read a value of size @size from offset @offset within the configuration
83  * space of the device identified by the bus, device & function numbers in @bdf
84  * on the PCI bus @bus.
85  */
pci_generic_ecam_read_config(const struct udevice * bus,pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)86 static int pci_generic_ecam_read_config(const struct udevice *bus,
87 					pci_dev_t bdf, uint offset,
88 					ulong *valuep, enum pci_size_t size)
89 {
90 	if (!pci_generic_ecam_addr_valid(bus, bdf)) {
91 		*valuep = pci_get_ff(size);
92 		return 0;
93 	}
94 
95 	return pci_generic_mmap_read_config(bus, pci_generic_ecam_conf_address,
96 					    bdf, offset, valuep, size);
97 }
98 
99 /**
100  * pci_generic_ecam_write_config() - Write to configuration space
101  * @bus: Pointer to the PCI bus
102  * @bdf: Identifies the PCIe device to access
103  * @offset: The offset into the device's configuration space
104  * @value: The value to write
105  * @size: Indicates the size of access to perform
106  *
107  * Write the value @value of size @size from offset @offset within the
108  * configuration space of the device identified by the bus, device & function
109  * numbers in @bdf on the PCI bus @bus.
110  */
pci_generic_ecam_write_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)111 static int pci_generic_ecam_write_config(struct udevice *bus, pci_dev_t bdf,
112 				    uint offset, ulong value,
113 				    enum pci_size_t size)
114 {
115 	if (!pci_generic_ecam_addr_valid(bus, bdf))
116 		return 0;
117 
118 	return pci_generic_mmap_write_config(bus, pci_generic_ecam_conf_address,
119 					     bdf, offset, value, size);
120 }
121 
122 /**
123  * pci_generic_ecam_of_to_plat() - Translate from DT to device state
124  * @dev: A pointer to the device being operated on
125  *
126  * Translate relevant data from the device tree pertaining to device @dev into
127  * state that the driver will later make use of. This state is stored in the
128  * device's private data structure.
129  *
130  * Return: 0 on success, else -EINVAL
131  */
pci_generic_ecam_of_to_plat(struct udevice * dev)132 static int pci_generic_ecam_of_to_plat(struct udevice *dev)
133 {
134 	struct generic_ecam_pcie *pcie = dev_get_priv(dev);
135 	struct fdt_resource reg_res;
136 	DECLARE_GLOBAL_DATA_PTR;
137 	int err;
138 
139 	err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
140 			       0, &reg_res);
141 	if (err < 0) {
142 		pr_err("\"reg\" resource not found\n");
143 		return err;
144 	}
145 
146 	pcie->size = fdt_resource_size(&reg_res);
147 	pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE);
148 
149 	return 0;
150 }
151 
pci_generic_ecam_probe(struct udevice * dev)152 static int pci_generic_ecam_probe(struct udevice *dev)
153 {
154 	struct generic_ecam_pcie *pcie = dev_get_priv(dev);
155 
156 	pcie->first_busno = dev_seq(dev);
157 
158 	return 0;
159 }
160 
161 static const struct dm_pci_ops pci_generic_ecam_ops = {
162 	.read_config	= pci_generic_ecam_read_config,
163 	.write_config	= pci_generic_ecam_write_config,
164 };
165 
166 static const struct udevice_id pci_generic_ecam_ids[] = {
167 	{ .compatible = "pci-host-ecam-generic" /* PCI-E */ },
168 	{ .compatible = "pci-host-cam-generic", .data = TYPE_PCI },
169 	{ }
170 };
171 
172 U_BOOT_DRIVER(pci_generic_ecam) = {
173 	.name			= "pci_generic_ecam",
174 	.id			= UCLASS_PCI,
175 	.of_match		= pci_generic_ecam_ids,
176 	.ops			= &pci_generic_ecam_ops,
177 	.probe			= pci_generic_ecam_probe,
178 	.of_to_plat	= pci_generic_ecam_of_to_plat,
179 	.priv_auto	= sizeof(struct generic_ecam_pcie),
180 };
181