xref: /OK3568_Linux_fs/u-boot/drivers/pci/pcie_xilinx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Xilinx AXI Bridge for PCI Express Driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2016 Imagination Technologies
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun #include <dm.h>
11*4882a593Smuzhiyun #include <pci.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <asm/io.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun  * struct xilinx_pcie - Xilinx PCIe controller state
17*4882a593Smuzhiyun  * @hose: The parent classes PCI controller state
18*4882a593Smuzhiyun  * @cfg_base: The base address of memory mapped configuration space
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun struct xilinx_pcie {
21*4882a593Smuzhiyun 	struct pci_controller hose;
22*4882a593Smuzhiyun 	void *cfg_base;
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /* Register definitions */
26*4882a593Smuzhiyun #define XILINX_PCIE_REG_PSCR		0x144
27*4882a593Smuzhiyun #define XILINX_PCIE_REG_PSCR_LNKUP	BIT(11)
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  * pcie_xilinx_link_up() - Check whether the PCIe link is up
31*4882a593Smuzhiyun  * @pcie: Pointer to the PCI controller state
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Checks whether the PCIe link for the given device is up or down.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Return: true if the link is up, else false
36*4882a593Smuzhiyun  */
pcie_xilinx_link_up(struct xilinx_pcie * pcie)37*4882a593Smuzhiyun static bool pcie_xilinx_link_up(struct xilinx_pcie *pcie)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	uint32_t pscr = __raw_readl(pcie->cfg_base + XILINX_PCIE_REG_PSCR);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	return pscr & XILINX_PCIE_REG_PSCR_LNKUP;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun  * pcie_xilinx_config_address() - Calculate the address of a config access
46*4882a593Smuzhiyun  * @pcie: Pointer to the PCI controller state
47*4882a593Smuzhiyun  * @bdf: Identifies the PCIe device to access
48*4882a593Smuzhiyun  * @offset: The offset into the device's configuration space
49*4882a593Smuzhiyun  * @paddress: Pointer to the pointer to write the calculates address to
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Calculates the address that should be accessed to perform a PCIe
52*4882a593Smuzhiyun  * configuration space access for a given device identified by the PCIe
53*4882a593Smuzhiyun  * controller device @pcie and the bus, device & function numbers in @bdf. If
54*4882a593Smuzhiyun  * access to the device is not valid then the function will return an error
55*4882a593Smuzhiyun  * code. Otherwise the address to access will be written to the pointer pointed
56*4882a593Smuzhiyun  * to by @paddress.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * Return: 0 on success, else -ENODEV
59*4882a593Smuzhiyun  */
pcie_xilinx_config_address(struct xilinx_pcie * pcie,pci_dev_t bdf,uint offset,void ** paddress)60*4882a593Smuzhiyun static int pcie_xilinx_config_address(struct xilinx_pcie *pcie, pci_dev_t bdf,
61*4882a593Smuzhiyun 				      uint offset, void **paddress)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	unsigned int bus = PCI_BUS(bdf);
64*4882a593Smuzhiyun 	unsigned int dev = PCI_DEV(bdf);
65*4882a593Smuzhiyun 	unsigned int func = PCI_FUNC(bdf);
66*4882a593Smuzhiyun 	void *addr;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if ((bus > 0) && !pcie_xilinx_link_up(pcie))
69*4882a593Smuzhiyun 		return -ENODEV;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	/*
72*4882a593Smuzhiyun 	 * Busses 0 (host-PCIe bridge) & 1 (its immediate child) are
73*4882a593Smuzhiyun 	 * limited to a single device each.
74*4882a593Smuzhiyun 	 */
75*4882a593Smuzhiyun 	if ((bus < 2) && (dev > 0))
76*4882a593Smuzhiyun 		return -ENODEV;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	addr = pcie->cfg_base;
79*4882a593Smuzhiyun 	addr += bus << 20;
80*4882a593Smuzhiyun 	addr += dev << 15;
81*4882a593Smuzhiyun 	addr += func << 12;
82*4882a593Smuzhiyun 	addr += offset;
83*4882a593Smuzhiyun 	*paddress = addr;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return 0;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /**
89*4882a593Smuzhiyun  * pcie_xilinx_read_config() - Read from configuration space
90*4882a593Smuzhiyun  * @pcie: Pointer to the PCI controller state
91*4882a593Smuzhiyun  * @bdf: Identifies the PCIe device to access
92*4882a593Smuzhiyun  * @offset: The offset into the device's configuration space
93*4882a593Smuzhiyun  * @valuep: A pointer at which to store the read value
94*4882a593Smuzhiyun  * @size: Indicates the size of access to perform
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  * Read a value of size @size from offset @offset within the configuration
97*4882a593Smuzhiyun  * space of the device identified by the bus, device & function numbers in @bdf
98*4882a593Smuzhiyun  * on the PCI bus @bus.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * Return: 0 on success, else -ENODEV or -EINVAL
101*4882a593Smuzhiyun  */
pcie_xilinx_read_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)102*4882a593Smuzhiyun static int pcie_xilinx_read_config(struct udevice *bus, pci_dev_t bdf,
103*4882a593Smuzhiyun 				   uint offset, ulong *valuep,
104*4882a593Smuzhiyun 				   enum pci_size_t size)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	struct xilinx_pcie *pcie = dev_get_priv(bus);
107*4882a593Smuzhiyun 	void *address;
108*4882a593Smuzhiyun 	int err;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	err = pcie_xilinx_config_address(pcie, bdf, offset, &address);
111*4882a593Smuzhiyun 	if (err < 0) {
112*4882a593Smuzhiyun 		*valuep = pci_get_ff(size);
113*4882a593Smuzhiyun 		return 0;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	switch (size) {
117*4882a593Smuzhiyun 	case PCI_SIZE_8:
118*4882a593Smuzhiyun 		*valuep = __raw_readb(address);
119*4882a593Smuzhiyun 		return 0;
120*4882a593Smuzhiyun 	case PCI_SIZE_16:
121*4882a593Smuzhiyun 		*valuep = __raw_readw(address);
122*4882a593Smuzhiyun 		return 0;
123*4882a593Smuzhiyun 	case PCI_SIZE_32:
124*4882a593Smuzhiyun 		*valuep = __raw_readl(address);
125*4882a593Smuzhiyun 		return 0;
126*4882a593Smuzhiyun 	default:
127*4882a593Smuzhiyun 		return -EINVAL;
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /**
132*4882a593Smuzhiyun  * pcie_xilinx_write_config() - Write to configuration space
133*4882a593Smuzhiyun  * @pcie: Pointer to the PCI controller state
134*4882a593Smuzhiyun  * @bdf: Identifies the PCIe device to access
135*4882a593Smuzhiyun  * @offset: The offset into the device's configuration space
136*4882a593Smuzhiyun  * @value: The value to write
137*4882a593Smuzhiyun  * @size: Indicates the size of access to perform
138*4882a593Smuzhiyun  *
139*4882a593Smuzhiyun  * Write the value @value of size @size from offset @offset within the
140*4882a593Smuzhiyun  * configuration space of the device identified by the bus, device & function
141*4882a593Smuzhiyun  * numbers in @bdf on the PCI bus @bus.
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * Return: 0 on success, else -ENODEV or -EINVAL
144*4882a593Smuzhiyun  */
pcie_xilinx_write_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)145*4882a593Smuzhiyun static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf,
146*4882a593Smuzhiyun 				    uint offset, ulong value,
147*4882a593Smuzhiyun 				    enum pci_size_t size)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	struct xilinx_pcie *pcie = dev_get_priv(bus);
150*4882a593Smuzhiyun 	void *address;
151*4882a593Smuzhiyun 	int err;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	err = pcie_xilinx_config_address(pcie, bdf, offset, &address);
154*4882a593Smuzhiyun 	if (err < 0)
155*4882a593Smuzhiyun 		return 0;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	switch (size) {
158*4882a593Smuzhiyun 	case PCI_SIZE_8:
159*4882a593Smuzhiyun 		__raw_writeb(value, address);
160*4882a593Smuzhiyun 		return 0;
161*4882a593Smuzhiyun 	case PCI_SIZE_16:
162*4882a593Smuzhiyun 		__raw_writew(value, address);
163*4882a593Smuzhiyun 		return 0;
164*4882a593Smuzhiyun 	case PCI_SIZE_32:
165*4882a593Smuzhiyun 		__raw_writel(value, address);
166*4882a593Smuzhiyun 		return 0;
167*4882a593Smuzhiyun 	default:
168*4882a593Smuzhiyun 		return -EINVAL;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun /**
173*4882a593Smuzhiyun  * pcie_xilinx_ofdata_to_platdata() - Translate from DT to device state
174*4882a593Smuzhiyun  * @dev: A pointer to the device being operated on
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  * Translate relevant data from the device tree pertaining to device @dev into
177*4882a593Smuzhiyun  * state that the driver will later make use of. This state is stored in the
178*4882a593Smuzhiyun  * device's private data structure.
179*4882a593Smuzhiyun  *
180*4882a593Smuzhiyun  * Return: 0 on success, else -EINVAL
181*4882a593Smuzhiyun  */
pcie_xilinx_ofdata_to_platdata(struct udevice * dev)182*4882a593Smuzhiyun static int pcie_xilinx_ofdata_to_platdata(struct udevice *dev)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	struct xilinx_pcie *pcie = dev_get_priv(dev);
185*4882a593Smuzhiyun 	struct fdt_resource reg_res;
186*4882a593Smuzhiyun 	DECLARE_GLOBAL_DATA_PTR;
187*4882a593Smuzhiyun 	int err;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
190*4882a593Smuzhiyun 			       0, &reg_res);
191*4882a593Smuzhiyun 	if (err < 0) {
192*4882a593Smuzhiyun 		pr_err("\"reg\" resource not found\n");
193*4882a593Smuzhiyun 		return err;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	pcie->cfg_base = map_physmem(reg_res.start,
197*4882a593Smuzhiyun 				     fdt_resource_size(&reg_res),
198*4882a593Smuzhiyun 				     MAP_NOCACHE);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	return 0;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun static const struct dm_pci_ops pcie_xilinx_ops = {
204*4882a593Smuzhiyun 	.read_config	= pcie_xilinx_read_config,
205*4882a593Smuzhiyun 	.write_config	= pcie_xilinx_write_config,
206*4882a593Smuzhiyun };
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun static const struct udevice_id pcie_xilinx_ids[] = {
209*4882a593Smuzhiyun 	{ .compatible = "xlnx,axi-pcie-host-1.00.a" },
210*4882a593Smuzhiyun 	{ }
211*4882a593Smuzhiyun };
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun U_BOOT_DRIVER(pcie_xilinx) = {
214*4882a593Smuzhiyun 	.name			= "pcie_xilinx",
215*4882a593Smuzhiyun 	.id			= UCLASS_PCI,
216*4882a593Smuzhiyun 	.of_match		= pcie_xilinx_ids,
217*4882a593Smuzhiyun 	.ops			= &pcie_xilinx_ops,
218*4882a593Smuzhiyun 	.ofdata_to_platdata	= pcie_xilinx_ofdata_to_platdata,
219*4882a593Smuzhiyun 	.priv_auto_alloc_size	= sizeof(struct xilinx_pcie),
220*4882a593Smuzhiyun };
221