blob: c07feba79768e971155f3dba9a722e7f081ec975 [file] [log] [blame]
Heiko Schocherb61cbbd2019-10-14 11:29:39 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2019
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 *
6 */
Simon Glasscd93d622020-05-10 11:40:13 -06007#include <asm/bitops.h>
Heiko Schocherb61cbbd2019-10-14 11:29:39 +02008#include <pci.h>
9#include <dm.h>
10#include <asm/fsl_law.h>
11
12struct mpc85xx_pci_priv {
13 void __iomem *cfg_addr;
14 void __iomem *cfg_data;
15};
16
Simon Glassc4e72c42020-01-27 08:49:37 -070017static int mpc85xx_pci_dm_read_config(const struct udevice *dev, pci_dev_t bdf,
Heiko Schocherb61cbbd2019-10-14 11:29:39 +020018 uint offset, ulong *value,
19 enum pci_size_t size)
20{
21 struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
22 u32 addr;
23
Pali Rohár159f1042023-04-13 22:41:46 +020024 if (offset > 0xff) {
25 *value = pci_get_ff(size);
26 return 0;
27 }
28
Pali Rohárc06597f2023-04-20 21:44:23 +020029 /* Skip mpc85xx PCI controller's ATMU inbound registers */
30 if (PCI_BUS(bdf) == 0 && PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
31 (offset & ~3) >= PCI_BASE_ADDRESS_0 && (offset & ~3) <= PCI_BASE_ADDRESS_5) {
32 *value = 0;
33 return 0;
34 }
35
Pali Rohár159f1042023-04-13 22:41:46 +020036 addr = PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset);
Heiko Schocherb61cbbd2019-10-14 11:29:39 +020037 out_be32(priv->cfg_addr, addr);
38 sync();
Pali Rohárf5375a42023-04-13 22:41:45 +020039
40 switch (size) {
41 case PCI_SIZE_8:
42 *value = in_8(priv->cfg_data + (offset & 3));
43 break;
44 case PCI_SIZE_16:
45 *value = in_le16(priv->cfg_data + (offset & 2));
46 break;
47 case PCI_SIZE_32:
48 *value = in_le32(priv->cfg_data);
49 break;
50 }
Heiko Schocherb61cbbd2019-10-14 11:29:39 +020051
52 return 0;
53}
54
55static int mpc85xx_pci_dm_write_config(struct udevice *dev, pci_dev_t bdf,
56 uint offset, ulong value,
57 enum pci_size_t size)
58{
59 struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
60 u32 addr;
61
Pali Rohár159f1042023-04-13 22:41:46 +020062 if (offset > 0xff)
63 return 0;
64
Pali Rohárc06597f2023-04-20 21:44:23 +020065 /* Skip mpc85xx PCI controller's ATMU inbound registers */
66 if (PCI_BUS(bdf) == 0 && PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
67 (offset & ~3) >= PCI_BASE_ADDRESS_0 && (offset & ~3) <= PCI_BASE_ADDRESS_5)
68 return 0;
69
Pali Rohár159f1042023-04-13 22:41:46 +020070 addr = PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset);
Heiko Schocherb61cbbd2019-10-14 11:29:39 +020071 out_be32(priv->cfg_addr, addr);
72 sync();
Pali Rohárf5375a42023-04-13 22:41:45 +020073
74 switch (size) {
75 case PCI_SIZE_8:
76 out_8(priv->cfg_data + (offset & 3), value);
77 break;
78 case PCI_SIZE_16:
79 out_le16(priv->cfg_data + (offset & 2), value);
80 break;
81 case PCI_SIZE_32:
82 out_le32(priv->cfg_data, value);
83 break;
84 }
Pali Rohár76c72932023-04-13 22:41:44 +020085 sync();
Heiko Schocherb61cbbd2019-10-14 11:29:39 +020086
87 return 0;
88}
89
Bin Meng5a40f5c2021-02-25 17:22:40 +080090#ifdef CONFIG_FSL_LAW
Heiko Schocherb61cbbd2019-10-14 11:29:39 +020091static int
92mpc85xx_pci_dm_setup_laws(struct pci_region *io, struct pci_region *mem,
93 struct pci_region *pre)
94{
95 /*
96 * Unfortunately we have defines for this addresse,
97 * as we have to setup the TLB, and at this stage
98 * we have no access to DT ... may we check here
99 * if the value in the define is the same ?
100 */
101 if (mem)
102 set_next_law(mem->phys_start, law_size_bits(mem->size),
103 LAW_TRGT_IF_PCI);
104 if (io)
105 set_next_law(io->phys_start, law_size_bits(io->size),
106 LAW_TRGT_IF_PCI);
107 if (pre)
108 set_next_law(pre->phys_start, law_size_bits(pre->size),
109 LAW_TRGT_IF_PCI);
110
111 return 0;
112}
Bin Meng5a40f5c2021-02-25 17:22:40 +0800113#endif
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200114
115static int mpc85xx_pci_dm_probe(struct udevice *dev)
116{
117 struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
118 struct pci_region *io;
119 struct pci_region *mem;
120 struct pci_region *pre;
121 int count;
122 ccsr_pcix_t *pcix;
123
124 count = pci_get_regions(dev, &io, &mem, &pre);
125 if (count != 2) {
126 printf("%s: wrong count of regions %d only 2 allowed\n",
127 __func__, count);
128 return -EINVAL;
129 }
130
Bin Meng5a40f5c2021-02-25 17:22:40 +0800131#ifdef CONFIG_FSL_LAW
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200132 mpc85xx_pci_dm_setup_laws(io, mem, pre);
Bin Meng5a40f5c2021-02-25 17:22:40 +0800133#endif
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200134
135 pcix = priv->cfg_addr;
136 /* BAR 1: memory */
Bin Meng8461ee52021-02-25 17:22:42 +0800137 out_be32(&pcix->potar1, mem->bus_start >> 12);
138 out_be32(&pcix->potear1, (u64)mem->bus_start >> 44);
139 out_be32(&pcix->powbar1, mem->phys_start >> 12);
140 out_be32(&pcix->powbear1, (u64)mem->phys_start >> 44);
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200141 out_be32(&pcix->powar1, (POWAR_EN | POWAR_MEM_READ |
142 POWAR_MEM_WRITE | (__ilog2(mem->size) - 1)));
143
144 /* BAR 1: IO */
Bin Meng8461ee52021-02-25 17:22:42 +0800145 out_be32(&pcix->potar2, io->bus_start >> 12);
146 out_be32(&pcix->potear2, (u64)io->bus_start >> 44);
147 out_be32(&pcix->powbar2, io->phys_start >> 12);
148 out_be32(&pcix->powbear2, (u64)io->phys_start >> 44);
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200149 out_be32(&pcix->powar2, (POWAR_EN | POWAR_IO_READ |
150 POWAR_IO_WRITE | (__ilog2(io->size) - 1)));
151
152 out_be32(&pcix->pitar1, 0);
153 out_be32(&pcix->piwbar1, 0);
154 out_be32(&pcix->piwar1, (PIWAR_EN | PIWAR_PF | PIWAR_LOCAL |
155 PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP | PIWAR_MEM_2G));
156
157 out_be32(&pcix->powar3, 0);
158 out_be32(&pcix->powar4, 0);
159 out_be32(&pcix->piwar2, 0);
160 out_be32(&pcix->piwar3, 0);
161
162 return 0;
163}
164
165static int mpc85xx_pci_dm_remove(struct udevice *dev)
166{
167 return 0;
168}
169
Simon Glassd1998a92020-12-03 16:55:21 -0700170static int mpc85xx_pci_of_to_plat(struct udevice *dev)
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200171{
172 struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
173 fdt_addr_t addr;
174
175 addr = devfdt_get_addr_index(dev, 0);
176 if (addr == FDT_ADDR_T_NONE)
177 return -EINVAL;
Bin Meng03ff9702021-02-25 17:22:41 +0800178 priv->cfg_addr = (void __iomem *)map_physmem(addr, 0, MAP_NOCACHE);
179 priv->cfg_data = (void __iomem *)((ulong)priv->cfg_addr + 4);
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200180
181 return 0;
182}
183
184static const struct dm_pci_ops mpc85xx_pci_ops = {
185 .read_config = mpc85xx_pci_dm_read_config,
186 .write_config = mpc85xx_pci_dm_write_config,
187};
188
189static const struct udevice_id mpc85xx_pci_ids[] = {
190 { .compatible = "fsl,mpc8540-pci" },
191 { }
192};
193
194U_BOOT_DRIVER(mpc85xx_pci) = {
195 .name = "mpc85xx_pci",
196 .id = UCLASS_PCI,
197 .of_match = mpc85xx_pci_ids,
198 .ops = &mpc85xx_pci_ops,
199 .probe = mpc85xx_pci_dm_probe,
200 .remove = mpc85xx_pci_dm_remove,
Simon Glassd1998a92020-12-03 16:55:21 -0700201 .of_to_plat = mpc85xx_pci_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700202 .priv_auto = sizeof(struct mpc85xx_pci_priv),
Heiko Schocherb61cbbd2019-10-14 11:29:39 +0200203};