Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_PCI |
| 8 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 12 | #include <init.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 15 | #include <pci.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 17 | #include <asm/io.h> |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
Simon Glass | bf50159 | 2017-05-18 20:09:51 -0600 | [diff] [blame] | 19 | #include <dm/lists.h> |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 20 | #include <dm/uclass-internal.h> |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 21 | #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) |
Simon Glass | 07f2f58 | 2019-08-24 14:19:05 -0600 | [diff] [blame] | 22 | #include <asm/fsp/fsp_support.h> |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 23 | #endif |
Simon Glass | f5cbb5c | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 24 | #include <dt-bindings/pci/pci.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 25 | #include <linux/delay.h> |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 26 | #include "pci_internal.h" |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Simon Glass | a6eb93b | 2016-01-18 20:19:14 -0700 | [diff] [blame] | 30 | int pci_get_bus(int busnum, struct udevice **busp) |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 31 | { |
| 32 | int ret; |
| 33 | |
| 34 | ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); |
| 35 | |
| 36 | /* Since buses may not be numbered yet try a little harder with bus 0 */ |
| 37 | if (ret == -ENODEV) { |
Simon Glass | 3f603cb | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 38 | ret = uclass_first_device_err(UCLASS_PCI, busp); |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 39 | if (ret) |
| 40 | return ret; |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 41 | ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); |
| 42 | } |
| 43 | |
| 44 | return ret; |
| 45 | } |
| 46 | |
Simon Glass | 9f60fb0 | 2015-11-19 20:27:00 -0700 | [diff] [blame] | 47 | struct udevice *pci_get_controller(struct udevice *dev) |
| 48 | { |
| 49 | while (device_is_on_pci_bus(dev)) |
| 50 | dev = dev->parent; |
| 51 | |
| 52 | return dev; |
| 53 | } |
| 54 | |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 55 | pci_dev_t dm_pci_get_bdf(const struct udevice *dev) |
Simon Glass | 4b515e4 | 2015-07-06 16:47:46 -0600 | [diff] [blame] | 56 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 57 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
Simon Glass | 4b515e4 | 2015-07-06 16:47:46 -0600 | [diff] [blame] | 58 | struct udevice *bus = dev->parent; |
| 59 | |
Simon Glass | 4886287 | 2019-12-29 21:19:14 -0700 | [diff] [blame] | 60 | /* |
| 61 | * This error indicates that @dev is a device on an unprobed PCI bus. |
| 62 | * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below |
| 63 | * will produce a bad BDF> |
| 64 | * |
| 65 | * A common cause of this problem is that this function is called in the |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 66 | * of_to_plat() method of @dev. Accessing the PCI bus in that |
Simon Glass | 4886287 | 2019-12-29 21:19:14 -0700 | [diff] [blame] | 67 | * method is not allowed, since it has not yet been probed. To fix this, |
| 68 | * move that access to the probe() method of @dev instead. |
| 69 | */ |
| 70 | if (!device_active(bus)) |
| 71 | log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name, |
| 72 | bus->name); |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 73 | return PCI_ADD_BUS(dev_seq(bus), pplat->devfn); |
Simon Glass | 4b515e4 | 2015-07-06 16:47:46 -0600 | [diff] [blame] | 74 | } |
| 75 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 76 | /** |
| 77 | * pci_get_bus_max() - returns the bus number of the last active bus |
| 78 | * |
| 79 | * @return last bus number, or -1 if no active buses |
| 80 | */ |
| 81 | static int pci_get_bus_max(void) |
| 82 | { |
| 83 | struct udevice *bus; |
| 84 | struct uclass *uc; |
| 85 | int ret = -1; |
| 86 | |
| 87 | ret = uclass_get(UCLASS_PCI, &uc); |
| 88 | uclass_foreach_dev(bus, uc) { |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 89 | if (dev_seq(bus) > ret) |
| 90 | ret = dev_seq(bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | debug("%s: ret=%d\n", __func__, ret); |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | int pci_last_busno(void) |
| 99 | { |
Bin Meng | 069155c | 2015-10-01 00:36:01 -0700 | [diff] [blame] | 100 | return pci_get_bus_max(); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int pci_get_ff(enum pci_size_t size) |
| 104 | { |
| 105 | switch (size) { |
| 106 | case PCI_SIZE_8: |
| 107 | return 0xff; |
| 108 | case PCI_SIZE_16: |
| 109 | return 0xffff; |
| 110 | default: |
| 111 | return 0xffffffff; |
| 112 | } |
| 113 | } |
| 114 | |
Marek Vasut | 02e4d38 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 115 | static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf, |
| 116 | ofnode *rnode) |
| 117 | { |
| 118 | struct fdt_pci_addr addr; |
| 119 | ofnode node; |
| 120 | int ret; |
| 121 | |
| 122 | dev_for_each_subnode(node, bus) { |
| 123 | ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg", |
| 124 | &addr); |
| 125 | if (ret) |
| 126 | continue; |
| 127 | |
| 128 | if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf)) |
| 129 | continue; |
| 130 | |
| 131 | *rnode = node; |
| 132 | break; |
| 133 | } |
| 134 | }; |
| 135 | |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 136 | int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 137 | struct udevice **devp) |
| 138 | { |
| 139 | struct udevice *dev; |
| 140 | |
| 141 | for (device_find_first_child(bus, &dev); |
| 142 | dev; |
| 143 | device_find_next_child(&dev)) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 144 | struct pci_child_plat *pplat; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 145 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 146 | pplat = dev_get_parent_plat(dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 147 | if (pplat && pplat->devfn == find_devfn) { |
| 148 | *devp = dev; |
| 149 | return 0; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return -ENODEV; |
| 154 | } |
| 155 | |
Simon Glass | f3f1fae | 2015-11-29 13:17:48 -0700 | [diff] [blame] | 156 | int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 157 | { |
| 158 | struct udevice *bus; |
| 159 | int ret; |
| 160 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 161 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 162 | if (ret) |
| 163 | return ret; |
| 164 | return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp); |
| 165 | } |
| 166 | |
| 167 | static int pci_device_matches_ids(struct udevice *dev, |
Simon Glass | e58f3a7 | 2021-06-27 17:50:56 -0600 | [diff] [blame] | 168 | const struct pci_device_id *ids) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 169 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 170 | struct pci_child_plat *pplat; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 171 | int i; |
| 172 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 173 | pplat = dev_get_parent_plat(dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 174 | if (!pplat) |
| 175 | return -EINVAL; |
| 176 | for (i = 0; ids[i].vendor != 0; i++) { |
| 177 | if (pplat->vendor == ids[i].vendor && |
| 178 | pplat->device == ids[i].device) |
| 179 | return i; |
| 180 | } |
| 181 | |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | |
Simon Glass | e58f3a7 | 2021-06-27 17:50:56 -0600 | [diff] [blame] | 185 | int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 186 | int *indexp, struct udevice **devp) |
| 187 | { |
| 188 | struct udevice *dev; |
| 189 | |
| 190 | /* Scan all devices on this bus */ |
| 191 | for (device_find_first_child(bus, &dev); |
| 192 | dev; |
| 193 | device_find_next_child(&dev)) { |
| 194 | if (pci_device_matches_ids(dev, ids) >= 0) { |
| 195 | if ((*indexp)-- <= 0) { |
| 196 | *devp = dev; |
| 197 | return 0; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
Simon Glass | e58f3a7 | 2021-06-27 17:50:56 -0600 | [diff] [blame] | 205 | int pci_find_device_id(const struct pci_device_id *ids, int index, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 206 | struct udevice **devp) |
| 207 | { |
| 208 | struct udevice *bus; |
| 209 | |
| 210 | /* Scan all known buses */ |
| 211 | for (uclass_first_device(UCLASS_PCI, &bus); |
| 212 | bus; |
| 213 | uclass_next_device(&bus)) { |
| 214 | if (!pci_bus_find_devices(bus, ids, &index, devp)) |
| 215 | return 0; |
| 216 | } |
| 217 | *devp = NULL; |
| 218 | |
| 219 | return -ENODEV; |
| 220 | } |
| 221 | |
Simon Glass | 5c0bf64 | 2015-11-29 13:17:50 -0700 | [diff] [blame] | 222 | static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor, |
| 223 | unsigned int device, int *indexp, |
| 224 | struct udevice **devp) |
| 225 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 226 | struct pci_child_plat *pplat; |
Simon Glass | 5c0bf64 | 2015-11-29 13:17:50 -0700 | [diff] [blame] | 227 | struct udevice *dev; |
| 228 | |
| 229 | for (device_find_first_child(bus, &dev); |
| 230 | dev; |
| 231 | device_find_next_child(&dev)) { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 232 | pplat = dev_get_parent_plat(dev); |
Simon Glass | 5c0bf64 | 2015-11-29 13:17:50 -0700 | [diff] [blame] | 233 | if (pplat->vendor == vendor && pplat->device == device) { |
| 234 | if (!(*indexp)--) { |
| 235 | *devp = dev; |
| 236 | return 0; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return -ENODEV; |
| 242 | } |
| 243 | |
| 244 | int dm_pci_find_device(unsigned int vendor, unsigned int device, int index, |
| 245 | struct udevice **devp) |
| 246 | { |
| 247 | struct udevice *bus; |
| 248 | |
| 249 | /* Scan all known buses */ |
| 250 | for (uclass_first_device(UCLASS_PCI, &bus); |
| 251 | bus; |
| 252 | uclass_next_device(&bus)) { |
| 253 | if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp)) |
| 254 | return device_probe(*devp); |
| 255 | } |
| 256 | *devp = NULL; |
| 257 | |
| 258 | return -ENODEV; |
| 259 | } |
| 260 | |
Simon Glass | a0eb835 | 2015-11-29 13:17:52 -0700 | [diff] [blame] | 261 | int dm_pci_find_class(uint find_class, int index, struct udevice **devp) |
| 262 | { |
| 263 | struct udevice *dev; |
| 264 | |
| 265 | /* Scan all known buses */ |
| 266 | for (pci_find_first_device(&dev); |
| 267 | dev; |
| 268 | pci_find_next_device(&dev)) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 269 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
Simon Glass | a0eb835 | 2015-11-29 13:17:52 -0700 | [diff] [blame] | 270 | |
| 271 | if (pplat->class == find_class && !index--) { |
| 272 | *devp = dev; |
| 273 | return device_probe(*devp); |
| 274 | } |
| 275 | } |
| 276 | *devp = NULL; |
| 277 | |
| 278 | return -ENODEV; |
| 279 | } |
| 280 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 281 | int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset, |
| 282 | unsigned long value, enum pci_size_t size) |
| 283 | { |
| 284 | struct dm_pci_ops *ops; |
| 285 | |
| 286 | ops = pci_get_ops(bus); |
| 287 | if (!ops->write_config) |
| 288 | return -ENOSYS; |
| 289 | return ops->write_config(bus, bdf, offset, value, size); |
| 290 | } |
| 291 | |
Simon Glass | 319dba1 | 2016-03-06 19:27:52 -0700 | [diff] [blame] | 292 | int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset, |
| 293 | u32 clr, u32 set) |
| 294 | { |
| 295 | ulong val; |
| 296 | int ret; |
| 297 | |
| 298 | ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | val &= ~clr; |
| 302 | val |= set; |
| 303 | |
| 304 | return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32); |
| 305 | } |
| 306 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 307 | int pci_write_config(pci_dev_t bdf, int offset, unsigned long value, |
| 308 | enum pci_size_t size) |
| 309 | { |
| 310 | struct udevice *bus; |
| 311 | int ret; |
| 312 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 313 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 314 | if (ret) |
| 315 | return ret; |
| 316 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 317 | return pci_bus_write_config(bus, bdf, offset, value, size); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 320 | int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value, |
| 321 | enum pci_size_t size) |
| 322 | { |
| 323 | struct udevice *bus; |
| 324 | |
Bin Meng | 1e0f226 | 2015-09-11 03:24:34 -0700 | [diff] [blame] | 325 | for (bus = dev; device_is_on_pci_bus(bus);) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 326 | bus = bus->parent; |
Simon Glass | 21ccce1 | 2015-11-29 13:17:47 -0700 | [diff] [blame] | 327 | return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value, |
| 328 | size); |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 329 | } |
| 330 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 331 | int pci_write_config32(pci_dev_t bdf, int offset, u32 value) |
| 332 | { |
| 333 | return pci_write_config(bdf, offset, value, PCI_SIZE_32); |
| 334 | } |
| 335 | |
| 336 | int pci_write_config16(pci_dev_t bdf, int offset, u16 value) |
| 337 | { |
| 338 | return pci_write_config(bdf, offset, value, PCI_SIZE_16); |
| 339 | } |
| 340 | |
| 341 | int pci_write_config8(pci_dev_t bdf, int offset, u8 value) |
| 342 | { |
| 343 | return pci_write_config(bdf, offset, value, PCI_SIZE_8); |
| 344 | } |
| 345 | |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 346 | int dm_pci_write_config8(struct udevice *dev, int offset, u8 value) |
| 347 | { |
| 348 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_8); |
| 349 | } |
| 350 | |
| 351 | int dm_pci_write_config16(struct udevice *dev, int offset, u16 value) |
| 352 | { |
| 353 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_16); |
| 354 | } |
| 355 | |
| 356 | int dm_pci_write_config32(struct udevice *dev, int offset, u32 value) |
| 357 | { |
| 358 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_32); |
| 359 | } |
| 360 | |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 361 | int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 362 | unsigned long *valuep, enum pci_size_t size) |
| 363 | { |
| 364 | struct dm_pci_ops *ops; |
| 365 | |
| 366 | ops = pci_get_ops(bus); |
| 367 | if (!ops->read_config) |
| 368 | return -ENOSYS; |
| 369 | return ops->read_config(bus, bdf, offset, valuep, size); |
| 370 | } |
| 371 | |
| 372 | int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep, |
| 373 | enum pci_size_t size) |
| 374 | { |
| 375 | struct udevice *bus; |
| 376 | int ret; |
| 377 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 378 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 379 | if (ret) |
| 380 | return ret; |
| 381 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 382 | return pci_bus_read_config(bus, bdf, offset, valuep, size); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 385 | int dm_pci_read_config(const struct udevice *dev, int offset, |
| 386 | unsigned long *valuep, enum pci_size_t size) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 387 | { |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 388 | const struct udevice *bus; |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 389 | |
Bin Meng | 1e0f226 | 2015-09-11 03:24:34 -0700 | [diff] [blame] | 390 | for (bus = dev; device_is_on_pci_bus(bus);) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 391 | bus = bus->parent; |
Simon Glass | 21ccce1 | 2015-11-29 13:17:47 -0700 | [diff] [blame] | 392 | return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep, |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 393 | size); |
| 394 | } |
| 395 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 396 | int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep) |
| 397 | { |
| 398 | unsigned long value; |
| 399 | int ret; |
| 400 | |
| 401 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32); |
| 402 | if (ret) |
| 403 | return ret; |
| 404 | *valuep = value; |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep) |
| 410 | { |
| 411 | unsigned long value; |
| 412 | int ret; |
| 413 | |
| 414 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16); |
| 415 | if (ret) |
| 416 | return ret; |
| 417 | *valuep = value; |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep) |
| 423 | { |
| 424 | unsigned long value; |
| 425 | int ret; |
| 426 | |
| 427 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8); |
| 428 | if (ret) |
| 429 | return ret; |
| 430 | *valuep = value; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 435 | int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 436 | { |
| 437 | unsigned long value; |
| 438 | int ret; |
| 439 | |
| 440 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8); |
| 441 | if (ret) |
| 442 | return ret; |
| 443 | *valuep = value; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 448 | int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 449 | { |
| 450 | unsigned long value; |
| 451 | int ret; |
| 452 | |
| 453 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | *valuep = value; |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 461 | int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 462 | { |
| 463 | unsigned long value; |
| 464 | int ret; |
| 465 | |
| 466 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32); |
| 467 | if (ret) |
| 468 | return ret; |
| 469 | *valuep = value; |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Simon Glass | 319dba1 | 2016-03-06 19:27:52 -0700 | [diff] [blame] | 474 | int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set) |
| 475 | { |
| 476 | u8 val; |
| 477 | int ret; |
| 478 | |
| 479 | ret = dm_pci_read_config8(dev, offset, &val); |
| 480 | if (ret) |
| 481 | return ret; |
| 482 | val &= ~clr; |
| 483 | val |= set; |
| 484 | |
| 485 | return dm_pci_write_config8(dev, offset, val); |
| 486 | } |
| 487 | |
| 488 | int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set) |
| 489 | { |
| 490 | u16 val; |
| 491 | int ret; |
| 492 | |
| 493 | ret = dm_pci_read_config16(dev, offset, &val); |
| 494 | if (ret) |
| 495 | return ret; |
| 496 | val &= ~clr; |
| 497 | val |= set; |
| 498 | |
| 499 | return dm_pci_write_config16(dev, offset, val); |
| 500 | } |
| 501 | |
| 502 | int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set) |
| 503 | { |
| 504 | u32 val; |
| 505 | int ret; |
| 506 | |
| 507 | ret = dm_pci_read_config32(dev, offset, &val); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | val &= ~clr; |
| 511 | val |= set; |
| 512 | |
| 513 | return dm_pci_write_config32(dev, offset, val); |
| 514 | } |
| 515 | |
Bin Meng | bbbcb52 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 516 | static void set_vga_bridge_bits(struct udevice *dev) |
| 517 | { |
| 518 | struct udevice *parent = dev->parent; |
| 519 | u16 bc; |
| 520 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 521 | while (dev_seq(parent) != 0) { |
Bin Meng | bbbcb52 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 522 | dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc); |
| 523 | bc |= PCI_BRIDGE_CTL_VGA; |
| 524 | dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc); |
| 525 | parent = parent->parent; |
| 526 | } |
| 527 | } |
| 528 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 529 | int pci_auto_config_devices(struct udevice *bus) |
| 530 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 531 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 532 | struct pci_child_plat *pplat; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 533 | unsigned int sub_bus; |
| 534 | struct udevice *dev; |
| 535 | int ret; |
| 536 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 537 | sub_bus = dev_seq(bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 538 | debug("%s: start\n", __func__); |
| 539 | pciauto_config_init(hose); |
| 540 | for (ret = device_find_first_child(bus, &dev); |
| 541 | !ret && dev; |
| 542 | ret = device_find_next_child(&dev)) { |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 543 | unsigned int max_bus; |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 544 | int ret; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 545 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 546 | debug("%s: device %s\n", __func__, dev->name); |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 547 | if (dev_has_ofnode(dev) && |
Suneel Garapati | f0c3692 | 2020-05-04 21:25:25 -0700 | [diff] [blame] | 548 | dev_read_bool(dev, "pci,no-autoconfig")) |
Simon Glass | d8c7fb5 | 2020-04-08 16:57:26 -0600 | [diff] [blame] | 549 | continue; |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 550 | ret = dm_pciauto_config_device(dev); |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 551 | if (ret < 0) |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 552 | return log_msg_ret("auto", ret); |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 553 | max_bus = ret; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 554 | sub_bus = max(sub_bus, max_bus); |
Bin Meng | bbbcb52 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 555 | |
Masami Hiramatsu | 2f7dddc | 2021-06-04 18:43:34 +0900 | [diff] [blame] | 556 | if (dev_get_parent(dev) == bus) |
| 557 | continue; |
| 558 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 559 | pplat = dev_get_parent_plat(dev); |
Bin Meng | bbbcb52 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 560 | if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8)) |
| 561 | set_vga_bridge_bits(dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 562 | } |
| 563 | debug("%s: done\n", __func__); |
| 564 | |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 565 | return log_msg_ret("sub", sub_bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Tuomas Tynkkynen | badb992 | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 568 | int pci_generic_mmap_write_config( |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 569 | const struct udevice *bus, |
| 570 | int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset, |
| 571 | void **addrp), |
Tuomas Tynkkynen | badb992 | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 572 | pci_dev_t bdf, |
| 573 | uint offset, |
| 574 | ulong value, |
| 575 | enum pci_size_t size) |
| 576 | { |
| 577 | void *address; |
| 578 | |
| 579 | if (addr_f(bus, bdf, offset, &address) < 0) |
| 580 | return 0; |
| 581 | |
| 582 | switch (size) { |
| 583 | case PCI_SIZE_8: |
| 584 | writeb(value, address); |
| 585 | return 0; |
| 586 | case PCI_SIZE_16: |
| 587 | writew(value, address); |
| 588 | return 0; |
| 589 | case PCI_SIZE_32: |
| 590 | writel(value, address); |
| 591 | return 0; |
| 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | int pci_generic_mmap_read_config( |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 598 | const struct udevice *bus, |
| 599 | int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset, |
| 600 | void **addrp), |
Tuomas Tynkkynen | badb992 | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 601 | pci_dev_t bdf, |
| 602 | uint offset, |
| 603 | ulong *valuep, |
| 604 | enum pci_size_t size) |
| 605 | { |
| 606 | void *address; |
| 607 | |
| 608 | if (addr_f(bus, bdf, offset, &address) < 0) { |
| 609 | *valuep = pci_get_ff(size); |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | switch (size) { |
| 614 | case PCI_SIZE_8: |
| 615 | *valuep = readb(address); |
| 616 | return 0; |
| 617 | case PCI_SIZE_16: |
| 618 | *valuep = readw(address); |
| 619 | return 0; |
| 620 | case PCI_SIZE_32: |
| 621 | *valuep = readl(address); |
| 622 | return 0; |
| 623 | default: |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | } |
| 627 | |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 628 | int dm_pci_hose_probe_bus(struct udevice *bus) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 629 | { |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 630 | int sub_bus; |
| 631 | int ret; |
Suneel Garapati | 636cc17 | 2019-10-19 15:52:32 -0700 | [diff] [blame] | 632 | int ea_pos; |
| 633 | u8 reg; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 634 | |
| 635 | debug("%s\n", __func__); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 636 | |
Suneel Garapati | 636cc17 | 2019-10-19 15:52:32 -0700 | [diff] [blame] | 637 | ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA); |
| 638 | if (ea_pos) { |
| 639 | dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8), |
| 640 | ®); |
| 641 | sub_bus = reg; |
| 642 | } else { |
| 643 | sub_bus = pci_get_bus_max() + 1; |
| 644 | } |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 645 | debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name); |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 646 | dm_pciauto_prescan_setup_bridge(bus, sub_bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 647 | |
| 648 | ret = device_probe(bus); |
| 649 | if (ret) { |
Simon Glass | 3129ace | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 650 | debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 651 | ret); |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 652 | return log_msg_ret("probe", ret); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 653 | } |
Suneel Garapati | 636cc17 | 2019-10-19 15:52:32 -0700 | [diff] [blame] | 654 | |
Masami Hiramatsu | 19e1b8d | 2021-04-16 14:53:46 -0700 | [diff] [blame] | 655 | if (!ea_pos) |
| 656 | sub_bus = pci_get_bus_max(); |
| 657 | |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 658 | dm_pciauto_postscan_setup_bridge(bus, sub_bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 659 | |
| 660 | return sub_bus; |
| 661 | } |
| 662 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 663 | /** |
| 664 | * pci_match_one_device - Tell if a PCI device structure has a matching |
| 665 | * PCI device id structure |
| 666 | * @id: single PCI device id structure to match |
Hou Zhiqiang | 0367bd4 | 2017-03-22 16:07:24 +0800 | [diff] [blame] | 667 | * @find: the PCI device id structure to match against |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 668 | * |
Hou Zhiqiang | 0367bd4 | 2017-03-22 16:07:24 +0800 | [diff] [blame] | 669 | * Returns true if the finding pci_device_id structure matched or false if |
| 670 | * there is no match. |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 671 | */ |
| 672 | static bool pci_match_one_id(const struct pci_device_id *id, |
| 673 | const struct pci_device_id *find) |
| 674 | { |
| 675 | if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) && |
| 676 | (id->device == PCI_ANY_ID || id->device == find->device) && |
| 677 | (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) && |
| 678 | (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) && |
| 679 | !((id->class ^ find->class) & id->class_mask)) |
| 680 | return true; |
| 681 | |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | /** |
Simon Glass | f5cbb5c | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 686 | * pci_need_device_pre_reloc() - Check if a device should be bound |
| 687 | * |
| 688 | * This checks a list of vendor/device-ID values indicating devices that should |
| 689 | * be bound before relocation. |
| 690 | * |
| 691 | * @bus: Bus to check |
| 692 | * @vendor: Vendor ID to check |
| 693 | * @device: Device ID to check |
| 694 | * @return true if the vendor/device is in the list, false if not |
| 695 | */ |
| 696 | static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor, |
| 697 | uint device) |
| 698 | { |
| 699 | u32 vendev; |
| 700 | int index; |
| 701 | |
| 702 | for (index = 0; |
| 703 | !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index, |
| 704 | &vendev); |
| 705 | index++) { |
| 706 | if (vendev == PCI_VENDEV(vendor, device)) |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | /** |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 714 | * pci_find_and_bind_driver() - Find and bind the right PCI driver |
| 715 | * |
| 716 | * This only looks at certain fields in the descriptor. |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 717 | * |
| 718 | * @parent: Parent bus |
| 719 | * @find_id: Specification of the driver to find |
| 720 | * @bdf: Bus/device/function addreess - see PCI_BDF() |
| 721 | * @devp: Returns a pointer to the device created |
| 722 | * @return 0 if OK, -EPERM if the device is not needed before relocation and |
| 723 | * therefore was not created, other -ve value on error |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 724 | */ |
| 725 | static int pci_find_and_bind_driver(struct udevice *parent, |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 726 | struct pci_device_id *find_id, |
| 727 | pci_dev_t bdf, struct udevice **devp) |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 728 | { |
| 729 | struct pci_driver_entry *start, *entry; |
Marek Vasut | 02e4d38 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 730 | ofnode node = ofnode_null(); |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 731 | const char *drv; |
| 732 | int n_ents; |
| 733 | int ret; |
| 734 | char name[30], *str; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 735 | bool bridge; |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 736 | |
| 737 | *devp = NULL; |
| 738 | |
| 739 | debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__, |
| 740 | find_id->vendor, find_id->device); |
Marek Vasut | 02e4d38 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 741 | |
| 742 | /* Determine optional OF node */ |
Suneel Garapati | bc30140 | 2019-10-19 16:02:48 -0700 | [diff] [blame] | 743 | if (ofnode_valid(dev_ofnode(parent))) |
| 744 | pci_dev_find_ofnode(parent, bdf, &node); |
Marek Vasut | 02e4d38 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 745 | |
Michael Walle | a6cd597 | 2019-12-01 17:45:18 +0100 | [diff] [blame] | 746 | if (ofnode_valid(node) && !ofnode_is_available(node)) { |
| 747 | debug("%s: Ignoring disabled device\n", __func__); |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 748 | return log_msg_ret("dis", -EPERM); |
Michael Walle | a6cd597 | 2019-12-01 17:45:18 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 751 | start = ll_entry_start(struct pci_driver_entry, pci_driver_entry); |
| 752 | n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry); |
| 753 | for (entry = start; entry != start + n_ents; entry++) { |
| 754 | const struct pci_device_id *id; |
| 755 | struct udevice *dev; |
| 756 | const struct driver *drv; |
| 757 | |
| 758 | for (id = entry->match; |
| 759 | id->vendor || id->subvendor || id->class_mask; |
| 760 | id++) { |
| 761 | if (!pci_match_one_id(id, find_id)) |
| 762 | continue; |
| 763 | |
| 764 | drv = entry->driver; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * In the pre-relocation phase, we only bind devices |
| 768 | * whose driver has the DM_FLAG_PRE_RELOC set, to save |
| 769 | * precious memory space as on some platforms as that |
| 770 | * space is pretty limited (ie: using Cache As RAM). |
| 771 | */ |
| 772 | if (!(gd->flags & GD_FLG_RELOC) && |
| 773 | !(drv->flags & DM_FLAG_PRE_RELOC)) |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 774 | return log_msg_ret("pre", -EPERM); |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 775 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 776 | /* |
| 777 | * We could pass the descriptor to the driver as |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 778 | * plat (instead of NULL) and allow its bind() |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 779 | * method to return -ENOENT if it doesn't support this |
| 780 | * device. That way we could continue the search to |
| 781 | * find another driver. For now this doesn't seem |
| 782 | * necesssary, so just bind the first match. |
| 783 | */ |
Simon Glass | 734206d | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 784 | ret = device_bind(parent, drv, drv->name, NULL, node, |
| 785 | &dev); |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 786 | if (ret) |
| 787 | goto error; |
| 788 | debug("%s: Match found: %s\n", __func__, drv->name); |
Bin Meng | ed698aa | 2018-08-03 01:14:44 -0700 | [diff] [blame] | 789 | dev->driver_data = id->driver_data; |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 790 | *devp = dev; |
| 791 | return 0; |
| 792 | } |
| 793 | } |
| 794 | |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 795 | bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI; |
| 796 | /* |
| 797 | * In the pre-relocation phase, we only bind bridge devices to save |
| 798 | * precious memory space as on some platforms as that space is pretty |
| 799 | * limited (ie: using Cache As RAM). |
| 800 | */ |
Simon Glass | f5cbb5c | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 801 | if (!(gd->flags & GD_FLG_RELOC) && !bridge && |
| 802 | !pci_need_device_pre_reloc(parent, find_id->vendor, |
| 803 | find_id->device)) |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 804 | return log_msg_ret("notbr", -EPERM); |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 805 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 806 | /* Bind a generic driver so that the device can be used */ |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 807 | sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf), |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 808 | PCI_FUNC(bdf)); |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 809 | str = strdup(name); |
| 810 | if (!str) |
| 811 | return -ENOMEM; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 812 | drv = bridge ? "pci_bridge_drv" : "pci_generic_drv"; |
| 813 | |
Marek Vasut | 02e4d38 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 814 | ret = device_bind_driver_to_node(parent, drv, str, node, devp); |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 815 | if (ret) { |
Simon Glass | 3129ace | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 816 | debug("%s: Failed to bind generic driver: %d\n", __func__, ret); |
xypron.glpk@gmx.de | c42640c | 2017-05-08 20:40:16 +0200 | [diff] [blame] | 817 | free(str); |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 818 | return ret; |
| 819 | } |
| 820 | debug("%s: No match found: bound generic driver instead\n", __func__); |
| 821 | |
| 822 | return 0; |
| 823 | |
| 824 | error: |
| 825 | debug("%s: No match found: error %d\n", __func__, ret); |
| 826 | return ret; |
| 827 | } |
| 828 | |
Tim Harvey | cecd013 | 2021-04-16 14:53:47 -0700 | [diff] [blame] | 829 | __weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev) |
| 830 | { |
| 831 | } |
| 832 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 833 | int pci_bind_bus_devices(struct udevice *bus) |
| 834 | { |
| 835 | ulong vendor, device; |
| 836 | ulong header_type; |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 837 | pci_dev_t bdf, end; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 838 | bool found_multi; |
Suneel Garapati | a3fac3f | 2019-10-23 18:40:36 -0700 | [diff] [blame] | 839 | int ari_off; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 840 | int ret; |
| 841 | |
| 842 | found_multi = false; |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 843 | end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1, |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 844 | PCI_MAX_PCI_FUNCTIONS - 1); |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 845 | for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end; |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 846 | bdf += PCI_BDF(0, 0, 1)) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 847 | struct pci_child_plat *pplat; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 848 | struct udevice *dev; |
| 849 | ulong class; |
| 850 | |
Bin Meng | 64e45f7 | 2018-08-03 01:14:37 -0700 | [diff] [blame] | 851 | if (!PCI_FUNC(bdf)) |
| 852 | found_multi = false; |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 853 | if (PCI_FUNC(bdf) && !found_multi) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 854 | continue; |
Hou Zhiqiang | 2a87f7f | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 855 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 856 | /* Check only the first access, we don't expect problems */ |
Hou Zhiqiang | 2a87f7f | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 857 | ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor, |
| 858 | PCI_SIZE_16); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 859 | if (ret) |
| 860 | goto error; |
Hou Zhiqiang | 2a87f7f | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 861 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 862 | if (vendor == 0xffff || vendor == 0x0000) |
| 863 | continue; |
| 864 | |
Hou Zhiqiang | 2a87f7f | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 865 | pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE, |
| 866 | &header_type, PCI_SIZE_8); |
| 867 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 868 | if (!PCI_FUNC(bdf)) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 869 | found_multi = header_type & 0x80; |
| 870 | |
Simon Glass | 0911569 | 2019-09-25 08:56:12 -0600 | [diff] [blame] | 871 | debug("%s: bus %d/%s: found device %x, function %d", __func__, |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 872 | dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf)); |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 873 | pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 874 | PCI_SIZE_16); |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 875 | pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class, |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 876 | PCI_SIZE_32); |
| 877 | class >>= 8; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 878 | |
| 879 | /* Find this device in the device tree */ |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 880 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev); |
Simon Glass | 0911569 | 2019-09-25 08:56:12 -0600 | [diff] [blame] | 881 | debug(": find ret=%d\n", ret); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 882 | |
Simon Glass | 8bd4252 | 2015-11-29 13:18:09 -0700 | [diff] [blame] | 883 | /* If nothing in the device tree, bind a device */ |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 884 | if (ret == -ENODEV) { |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 885 | struct pci_device_id find_id; |
| 886 | ulong val; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 887 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 888 | memset(&find_id, '\0', sizeof(find_id)); |
| 889 | find_id.vendor = vendor; |
| 890 | find_id.device = device; |
| 891 | find_id.class = class; |
| 892 | if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) { |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 893 | pci_bus_read_config(bus, bdf, |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 894 | PCI_SUBSYSTEM_VENDOR_ID, |
| 895 | &val, PCI_SIZE_32); |
| 896 | find_id.subvendor = val & 0xffff; |
| 897 | find_id.subdevice = val >> 16; |
| 898 | } |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 899 | ret = pci_find_and_bind_driver(bus, &find_id, bdf, |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 900 | &dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 901 | } |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 902 | if (ret == -EPERM) |
| 903 | continue; |
| 904 | else if (ret) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 905 | return ret; |
| 906 | |
| 907 | /* Update the platform data */ |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 908 | pplat = dev_get_parent_plat(dev); |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 909 | pplat->devfn = PCI_MASK_BUS(bdf); |
| 910 | pplat->vendor = vendor; |
| 911 | pplat->device = device; |
| 912 | pplat->class = class; |
Suneel Garapati | a3fac3f | 2019-10-23 18:40:36 -0700 | [diff] [blame] | 913 | |
| 914 | if (IS_ENABLED(CONFIG_PCI_ARID)) { |
| 915 | ari_off = dm_pci_find_ext_capability(dev, |
| 916 | PCI_EXT_CAP_ID_ARI); |
| 917 | if (ari_off) { |
| 918 | u16 ari_cap; |
| 919 | |
| 920 | /* |
| 921 | * Read Next Function number in ARI Cap |
| 922 | * Register |
| 923 | */ |
| 924 | dm_pci_read_config16(dev, ari_off + 4, |
| 925 | &ari_cap); |
| 926 | /* |
| 927 | * Update next scan on this function number, |
| 928 | * subtract 1 in BDF to satisfy loop increment. |
| 929 | */ |
| 930 | if (ari_cap & 0xff00) { |
| 931 | bdf = PCI_BDF(PCI_BUS(bdf), |
| 932 | PCI_DEV(ari_cap), |
| 933 | PCI_FUNC(ari_cap)); |
| 934 | bdf = bdf - 0x100; |
| 935 | } |
| 936 | } |
| 937 | } |
Tim Harvey | cecd013 | 2021-04-16 14:53:47 -0700 | [diff] [blame] | 938 | |
| 939 | board_pci_fixup_dev(bus, dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | return 0; |
| 943 | error: |
| 944 | printf("Cannot read bus configuration: %d\n", ret); |
| 945 | |
| 946 | return ret; |
| 947 | } |
| 948 | |
Christian Gmeiner | f2825f6 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 949 | static void decode_regions(struct pci_controller *hose, ofnode parent_node, |
| 950 | ofnode node) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 951 | { |
| 952 | int pci_addr_cells, addr_cells, size_cells; |
| 953 | int cells_per_record; |
Stefan Roese | dfaf6a5 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 954 | struct bd_info *bd; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 955 | const u32 *prop; |
Stefan Roese | e002474 | 2020-07-23 16:34:10 +0200 | [diff] [blame] | 956 | int max_regions; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 957 | int len; |
| 958 | int i; |
| 959 | |
Masahiro Yamada | 61e51ba | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 960 | prop = ofnode_get_property(node, "ranges", &len); |
Christian Gmeiner | f2825f6 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 961 | if (!prop) { |
| 962 | debug("%s: Cannot decode regions\n", __func__); |
| 963 | return; |
| 964 | } |
| 965 | |
Simon Glass | 878d68c | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 966 | pci_addr_cells = ofnode_read_simple_addr_cells(node); |
| 967 | addr_cells = ofnode_read_simple_addr_cells(parent_node); |
| 968 | size_cells = ofnode_read_simple_size_cells(node); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 969 | |
| 970 | /* PCI addresses are always 3-cells */ |
| 971 | len /= sizeof(u32); |
| 972 | cells_per_record = pci_addr_cells + addr_cells + size_cells; |
| 973 | hose->region_count = 0; |
| 974 | debug("%s: len=%d, cells_per_record=%d\n", __func__, len, |
| 975 | cells_per_record); |
Stefan Roese | e002474 | 2020-07-23 16:34:10 +0200 | [diff] [blame] | 976 | |
| 977 | /* Dynamically allocate the regions array */ |
| 978 | max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS; |
| 979 | hose->regions = (struct pci_region *) |
| 980 | calloc(1, max_regions * sizeof(struct pci_region)); |
| 981 | |
| 982 | for (i = 0; i < max_regions; i++, len -= cells_per_record) { |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 983 | u64 pci_addr, addr, size; |
| 984 | int space_code; |
| 985 | u32 flags; |
| 986 | int type; |
Simon Glass | 9526d83 | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 987 | int pos; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 988 | |
| 989 | if (len < cells_per_record) |
| 990 | break; |
| 991 | flags = fdt32_to_cpu(prop[0]); |
| 992 | space_code = (flags >> 24) & 3; |
| 993 | pci_addr = fdtdec_get_number(prop + 1, 2); |
| 994 | prop += pci_addr_cells; |
| 995 | addr = fdtdec_get_number(prop, addr_cells); |
| 996 | prop += addr_cells; |
| 997 | size = fdtdec_get_number(prop, size_cells); |
| 998 | prop += size_cells; |
Masahiro Yamada | dee37fc | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 999 | debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n", |
| 1000 | __func__, hose->region_count, pci_addr, addr, size, space_code); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1001 | if (space_code & 2) { |
| 1002 | type = flags & (1U << 30) ? PCI_REGION_PREFETCH : |
| 1003 | PCI_REGION_MEM; |
| 1004 | } else if (space_code & 1) { |
| 1005 | type = PCI_REGION_IO; |
| 1006 | } else { |
| 1007 | continue; |
| 1008 | } |
Tuomas Tynkkynen | 52ba907 | 2018-05-14 18:47:50 +0300 | [diff] [blame] | 1009 | |
| 1010 | if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) && |
| 1011 | type == PCI_REGION_MEM && upper_32_bits(pci_addr)) { |
| 1012 | debug(" - beyond the 32-bit boundary, ignoring\n"); |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
Simon Glass | 9526d83 | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 1016 | pos = -1; |
Suneel Garapati | 4cf56ec | 2019-10-19 17:10:20 -0700 | [diff] [blame] | 1017 | if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) { |
| 1018 | for (i = 0; i < hose->region_count; i++) { |
| 1019 | if (hose->regions[i].flags == type) |
| 1020 | pos = i; |
| 1021 | } |
Simon Glass | 9526d83 | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 1022 | } |
Suneel Garapati | 4cf56ec | 2019-10-19 17:10:20 -0700 | [diff] [blame] | 1023 | |
Simon Glass | 9526d83 | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 1024 | if (pos == -1) |
| 1025 | pos = hose->region_count++; |
| 1026 | debug(" - type=%d, pos=%d\n", type, pos); |
| 1027 | pci_set_region(hose->regions + pos, pci_addr, addr, size, type); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* Add a region for our local memory */ |
Stefan Roese | dfaf6a5 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 1031 | bd = gd->bd; |
Bin Meng | 1eaf780 | 2018-03-27 00:46:05 -0700 | [diff] [blame] | 1032 | if (!bd) |
Christian Gmeiner | f2825f6 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 1033 | return; |
Bin Meng | 1eaf780 | 2018-03-27 00:46:05 -0700 | [diff] [blame] | 1034 | |
Bernhard Messerklinger | 664758c | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 1035 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) { |
| 1036 | if (bd->bi_dram[i].size) { |
Daniel Schwierzeck | a45343a | 2021-07-15 20:53:56 +0200 | [diff] [blame] | 1037 | phys_addr_t start = bd->bi_dram[i].start; |
| 1038 | |
| 1039 | if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY)) |
| 1040 | start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start); |
| 1041 | |
Bernhard Messerklinger | 664758c | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 1042 | pci_set_region(hose->regions + hose->region_count++, |
Daniel Schwierzeck | a45343a | 2021-07-15 20:53:56 +0200 | [diff] [blame] | 1043 | start, start, bd->bi_dram[i].size, |
Bernhard Messerklinger | 664758c | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 1044 | PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
| 1045 | } |
| 1046 | } |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1047 | |
Christian Gmeiner | f2825f6 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 1048 | return; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static int pci_uclass_pre_probe(struct udevice *bus) |
| 1052 | { |
| 1053 | struct pci_controller *hose; |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1054 | struct uclass *uc; |
| 1055 | int ret; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1056 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1057 | debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1058 | bus->parent->name); |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1059 | hose = dev_get_uclass_priv(bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1060 | |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1061 | /* |
| 1062 | * Set the sequence number, if device_bind() doesn't. We want control |
| 1063 | * of this so that numbers are allocated as devices are probed. That |
| 1064 | * ensures that sub-bus numbered is correct (sub-buses must get numbers |
| 1065 | * higher than their parents) |
| 1066 | */ |
| 1067 | if (dev_seq(bus) == -1) { |
| 1068 | ret = uclass_get(UCLASS_PCI, &uc); |
| 1069 | if (ret) |
| 1070 | return ret; |
Simon Glass | 2462139 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1071 | bus->seq_ = uclass_find_next_free_seq(uc); |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1074 | /* For bridges, use the top-level PCI controller */ |
Paul Burton | 65f62b1 | 2016-09-08 07:47:32 +0100 | [diff] [blame] | 1075 | if (!device_is_on_pci_bus(bus)) { |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1076 | hose->ctlr = bus; |
Christian Gmeiner | f2825f6 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 1077 | decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus)); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1078 | } else { |
| 1079 | struct pci_controller *parent_hose; |
| 1080 | |
| 1081 | parent_hose = dev_get_uclass_priv(bus->parent); |
| 1082 | hose->ctlr = parent_hose->bus; |
| 1083 | } |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1084 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1085 | hose->bus = bus; |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1086 | hose->first_busno = dev_seq(bus); |
| 1087 | hose->last_busno = dev_seq(bus); |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 1088 | if (dev_has_ofnode(bus)) { |
Suneel Garapati | f0c3692 | 2020-05-04 21:25:25 -0700 | [diff] [blame] | 1089 | hose->skip_auto_config_until_reloc = |
| 1090 | dev_read_bool(bus, |
| 1091 | "u-boot,skip-auto-config-until-reloc"); |
| 1092 | } |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | static int pci_uclass_post_probe(struct udevice *bus) |
| 1098 | { |
Simon Glass | 2206ac2 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1099 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1100 | int ret; |
| 1101 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1102 | debug("%s: probing bus %d\n", __func__, dev_seq(bus)); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1103 | ret = pci_bind_bus_devices(bus); |
| 1104 | if (ret) |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1105 | return log_msg_ret("bind", ret); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1106 | |
Simon Glass | f1f4438 | 2020-04-26 09:12:56 -0600 | [diff] [blame] | 1107 | if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() && |
Simon Glass | 2206ac2 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1108 | (!hose->skip_auto_config_until_reloc || |
| 1109 | (gd->flags & GD_FLG_RELOC))) { |
| 1110 | ret = pci_auto_config_devices(bus); |
| 1111 | if (ret < 0) |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1112 | return log_msg_ret("cfg", ret); |
Simon Glass | 2206ac2 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1113 | } |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1114 | |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1115 | #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) |
| 1116 | /* |
| 1117 | * Per Intel FSP specification, we should call FSP notify API to |
| 1118 | * inform FSP that PCI enumeration has been done so that FSP will |
| 1119 | * do any necessary initialization as required by the chipset's |
| 1120 | * BIOS Writer's Guide (BWG). |
| 1121 | * |
| 1122 | * Unfortunately we have to put this call here as with driver model, |
| 1123 | * the enumeration is all done on a lazy basis as needed, so until |
| 1124 | * something is touched on PCI it won't happen. |
| 1125 | * |
| 1126 | * Note we only call this 1) after U-Boot is relocated, and 2) |
| 1127 | * root bus has finished probing. |
| 1128 | */ |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1129 | if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) { |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1130 | ret = fsp_init_phase_pci(); |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1131 | if (ret) |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1132 | return log_msg_ret("fsp", ret); |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1133 | } |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1134 | #endif |
| 1135 | |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1136 | return 0; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | static int pci_uclass_child_post_bind(struct udevice *dev) |
| 1140 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1141 | struct pci_child_plat *pplat; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1142 | |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 1143 | if (!dev_has_ofnode(dev)) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1144 | return 0; |
| 1145 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1146 | pplat = dev_get_parent_plat(dev); |
Bin Meng | 1f6b08b | 2018-08-03 01:14:36 -0700 | [diff] [blame] | 1147 | |
| 1148 | /* Extract vendor id and device id if available */ |
| 1149 | ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device); |
| 1150 | |
| 1151 | /* Extract the devfn from fdt_pci_addr */ |
Stefan Roese | b521420 | 2019-01-25 11:52:42 +0100 | [diff] [blame] | 1152 | pplat->devfn = pci_get_devfn(dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1153 | |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 1157 | static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf, |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 1158 | uint offset, ulong *valuep, |
| 1159 | enum pci_size_t size) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1160 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1161 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1162 | |
| 1163 | return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size); |
| 1164 | } |
| 1165 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 1166 | static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf, |
| 1167 | uint offset, ulong value, |
| 1168 | enum pci_size_t size) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1169 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1170 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1171 | |
| 1172 | return pci_bus_write_config(hose->ctlr, bdf, offset, value, size); |
| 1173 | } |
| 1174 | |
Simon Glass | 76c3fbc | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1175 | static int skip_to_next_device(struct udevice *bus, struct udevice **devp) |
| 1176 | { |
| 1177 | struct udevice *dev; |
| 1178 | int ret = 0; |
| 1179 | |
| 1180 | /* |
| 1181 | * Scan through all the PCI controllers. On x86 there will only be one |
| 1182 | * but that is not necessarily true on other hardware. |
| 1183 | */ |
| 1184 | do { |
| 1185 | device_find_first_child(bus, &dev); |
| 1186 | if (dev) { |
| 1187 | *devp = dev; |
| 1188 | return 0; |
| 1189 | } |
| 1190 | ret = uclass_next_device(&bus); |
| 1191 | if (ret) |
| 1192 | return ret; |
| 1193 | } while (bus); |
| 1194 | |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | int pci_find_next_device(struct udevice **devp) |
| 1199 | { |
| 1200 | struct udevice *child = *devp; |
| 1201 | struct udevice *bus = child->parent; |
| 1202 | int ret; |
| 1203 | |
| 1204 | /* First try all the siblings */ |
| 1205 | *devp = NULL; |
| 1206 | while (child) { |
| 1207 | device_find_next_child(&child); |
| 1208 | if (child) { |
| 1209 | *devp = child; |
| 1210 | return 0; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | /* We ran out of siblings. Try the next bus */ |
| 1215 | ret = uclass_next_device(&bus); |
| 1216 | if (ret) |
| 1217 | return ret; |
| 1218 | |
| 1219 | return bus ? skip_to_next_device(bus, devp) : 0; |
| 1220 | } |
| 1221 | |
| 1222 | int pci_find_first_device(struct udevice **devp) |
| 1223 | { |
| 1224 | struct udevice *bus; |
| 1225 | int ret; |
| 1226 | |
| 1227 | *devp = NULL; |
| 1228 | ret = uclass_first_device(UCLASS_PCI, &bus); |
| 1229 | if (ret) |
| 1230 | return ret; |
| 1231 | |
| 1232 | return skip_to_next_device(bus, devp); |
| 1233 | } |
| 1234 | |
Simon Glass | 9289db6 | 2015-11-19 20:26:59 -0700 | [diff] [blame] | 1235 | ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size) |
| 1236 | { |
| 1237 | switch (size) { |
| 1238 | case PCI_SIZE_8: |
| 1239 | return (value >> ((offset & 3) * 8)) & 0xff; |
| 1240 | case PCI_SIZE_16: |
| 1241 | return (value >> ((offset & 2) * 8)) & 0xffff; |
| 1242 | default: |
| 1243 | return value; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | ulong pci_conv_size_to_32(ulong old, ulong value, uint offset, |
| 1248 | enum pci_size_t size) |
| 1249 | { |
| 1250 | uint off_mask; |
| 1251 | uint val_mask, shift; |
| 1252 | ulong ldata, mask; |
| 1253 | |
| 1254 | switch (size) { |
| 1255 | case PCI_SIZE_8: |
| 1256 | off_mask = 3; |
| 1257 | val_mask = 0xff; |
| 1258 | break; |
| 1259 | case PCI_SIZE_16: |
| 1260 | off_mask = 2; |
| 1261 | val_mask = 0xffff; |
| 1262 | break; |
| 1263 | default: |
| 1264 | return value; |
| 1265 | } |
| 1266 | shift = (offset & off_mask) * 8; |
| 1267 | ldata = (value & val_mask) << shift; |
| 1268 | mask = val_mask << shift; |
| 1269 | value = (old & ~mask) | ldata; |
| 1270 | |
| 1271 | return value; |
| 1272 | } |
| 1273 | |
Rayagonda Kokatanur | 143eb5b | 2020-05-12 13:29:49 +0530 | [diff] [blame] | 1274 | int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index) |
| 1275 | { |
| 1276 | int pci_addr_cells, addr_cells, size_cells; |
| 1277 | int cells_per_record; |
| 1278 | const u32 *prop; |
| 1279 | int len; |
| 1280 | int i = 0; |
| 1281 | |
| 1282 | prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len); |
| 1283 | if (!prop) { |
| 1284 | log_err("PCI: Device '%s': Cannot decode dma-ranges\n", |
| 1285 | dev->name); |
| 1286 | return -EINVAL; |
| 1287 | } |
| 1288 | |
| 1289 | pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev)); |
| 1290 | addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent)); |
| 1291 | size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev)); |
| 1292 | |
| 1293 | /* PCI addresses are always 3-cells */ |
| 1294 | len /= sizeof(u32); |
| 1295 | cells_per_record = pci_addr_cells + addr_cells + size_cells; |
| 1296 | debug("%s: len=%d, cells_per_record=%d\n", __func__, len, |
| 1297 | cells_per_record); |
| 1298 | |
| 1299 | while (len) { |
| 1300 | memp->bus_start = fdtdec_get_number(prop + 1, 2); |
| 1301 | prop += pci_addr_cells; |
| 1302 | memp->phys_start = fdtdec_get_number(prop, addr_cells); |
| 1303 | prop += addr_cells; |
| 1304 | memp->size = fdtdec_get_number(prop, size_cells); |
| 1305 | prop += size_cells; |
| 1306 | |
| 1307 | if (i == index) |
| 1308 | return 0; |
| 1309 | i++; |
| 1310 | len -= cells_per_record; |
| 1311 | } |
| 1312 | |
| 1313 | return -EINVAL; |
| 1314 | } |
| 1315 | |
Simon Glass | f926033 | 2015-11-19 20:27:01 -0700 | [diff] [blame] | 1316 | int pci_get_regions(struct udevice *dev, struct pci_region **iop, |
| 1317 | struct pci_region **memp, struct pci_region **prefp) |
| 1318 | { |
| 1319 | struct udevice *bus = pci_get_controller(dev); |
| 1320 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
| 1321 | int i; |
| 1322 | |
| 1323 | *iop = NULL; |
| 1324 | *memp = NULL; |
| 1325 | *prefp = NULL; |
| 1326 | for (i = 0; i < hose->region_count; i++) { |
| 1327 | switch (hose->regions[i].flags) { |
| 1328 | case PCI_REGION_IO: |
| 1329 | if (!*iop || (*iop)->size < hose->regions[i].size) |
| 1330 | *iop = hose->regions + i; |
| 1331 | break; |
| 1332 | case PCI_REGION_MEM: |
| 1333 | if (!*memp || (*memp)->size < hose->regions[i].size) |
| 1334 | *memp = hose->regions + i; |
| 1335 | break; |
| 1336 | case (PCI_REGION_MEM | PCI_REGION_PREFETCH): |
| 1337 | if (!*prefp || (*prefp)->size < hose->regions[i].size) |
| 1338 | *prefp = hose->regions + i; |
| 1339 | break; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL); |
| 1344 | } |
| 1345 | |
Simon Glass | 194fca9 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 1346 | u32 dm_pci_read_bar32(const struct udevice *dev, int barnum) |
Simon Glass | bab17cf | 2015-11-29 13:17:53 -0700 | [diff] [blame] | 1347 | { |
| 1348 | u32 addr; |
| 1349 | int bar; |
| 1350 | |
| 1351 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 1352 | dm_pci_read_config32(dev, bar, &addr); |
Simon Glass | 9ece4b0 | 2020-04-09 10:27:36 -0600 | [diff] [blame] | 1353 | |
| 1354 | /* |
| 1355 | * If we get an invalid address, return this so that comparisons with |
| 1356 | * FDT_ADDR_T_NONE work correctly |
| 1357 | */ |
| 1358 | if (addr == 0xffffffff) |
| 1359 | return addr; |
| 1360 | else if (addr & PCI_BASE_ADDRESS_SPACE_IO) |
Simon Glass | bab17cf | 2015-11-29 13:17:53 -0700 | [diff] [blame] | 1361 | return addr & PCI_BASE_ADDRESS_IO_MASK; |
| 1362 | else |
| 1363 | return addr & PCI_BASE_ADDRESS_MEM_MASK; |
| 1364 | } |
| 1365 | |
Simon Glass | 9d731c8 | 2016-01-18 20:19:15 -0700 | [diff] [blame] | 1366 | void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr) |
| 1367 | { |
| 1368 | int bar; |
| 1369 | |
| 1370 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 1371 | dm_pci_write_config32(dev, bar, addr); |
| 1372 | } |
| 1373 | |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1374 | static int _dm_pci_bus_to_phys(struct udevice *ctlr, |
| 1375 | pci_addr_t bus_addr, unsigned long flags, |
| 1376 | unsigned long skip_mask, phys_addr_t *pa) |
| 1377 | { |
| 1378 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 1379 | struct pci_region *res; |
| 1380 | int i; |
| 1381 | |
Christian Gmeiner | 6f95d89 | 2018-06-10 06:25:06 -0700 | [diff] [blame] | 1382 | if (hose->region_count == 0) { |
| 1383 | *pa = bus_addr; |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1387 | for (i = 0; i < hose->region_count; i++) { |
| 1388 | res = &hose->regions[i]; |
| 1389 | |
| 1390 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 1391 | continue; |
| 1392 | |
| 1393 | if (res->flags & skip_mask) |
| 1394 | continue; |
| 1395 | |
| 1396 | if (bus_addr >= res->bus_start && |
| 1397 | (bus_addr - res->bus_start) < res->size) { |
| 1398 | *pa = (bus_addr - res->bus_start + res->phys_start); |
| 1399 | return 0; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | return 1; |
| 1404 | } |
| 1405 | |
| 1406 | phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, |
| 1407 | unsigned long flags) |
| 1408 | { |
| 1409 | phys_addr_t phys_addr = 0; |
| 1410 | struct udevice *ctlr; |
| 1411 | int ret; |
| 1412 | |
| 1413 | /* The root controller has the region information */ |
| 1414 | ctlr = pci_get_controller(dev); |
| 1415 | |
| 1416 | /* |
| 1417 | * if PCI_REGION_MEM is set we do a two pass search with preference |
| 1418 | * on matches that don't have PCI_REGION_SYS_MEMORY set |
| 1419 | */ |
| 1420 | if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { |
| 1421 | ret = _dm_pci_bus_to_phys(ctlr, bus_addr, |
| 1422 | flags, PCI_REGION_SYS_MEMORY, |
| 1423 | &phys_addr); |
| 1424 | if (!ret) |
| 1425 | return phys_addr; |
| 1426 | } |
| 1427 | |
| 1428 | ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr); |
| 1429 | |
| 1430 | if (ret) |
| 1431 | puts("pci_hose_bus_to_phys: invalid physical address\n"); |
| 1432 | |
| 1433 | return phys_addr; |
| 1434 | } |
| 1435 | |
| 1436 | int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, |
| 1437 | unsigned long flags, unsigned long skip_mask, |
| 1438 | pci_addr_t *ba) |
| 1439 | { |
| 1440 | struct pci_region *res; |
| 1441 | struct udevice *ctlr; |
| 1442 | pci_addr_t bus_addr; |
| 1443 | int i; |
| 1444 | struct pci_controller *hose; |
| 1445 | |
| 1446 | /* The root controller has the region information */ |
| 1447 | ctlr = pci_get_controller(dev); |
| 1448 | hose = dev_get_uclass_priv(ctlr); |
| 1449 | |
Christian Gmeiner | 6f95d89 | 2018-06-10 06:25:06 -0700 | [diff] [blame] | 1450 | if (hose->region_count == 0) { |
| 1451 | *ba = phys_addr; |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1455 | for (i = 0; i < hose->region_count; i++) { |
| 1456 | res = &hose->regions[i]; |
| 1457 | |
| 1458 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 1459 | continue; |
| 1460 | |
| 1461 | if (res->flags & skip_mask) |
| 1462 | continue; |
| 1463 | |
| 1464 | bus_addr = phys_addr - res->phys_start + res->bus_start; |
| 1465 | |
| 1466 | if (bus_addr >= res->bus_start && |
| 1467 | (bus_addr - res->bus_start) < res->size) { |
| 1468 | *ba = bus_addr; |
| 1469 | return 0; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | return 1; |
| 1474 | } |
| 1475 | |
| 1476 | pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, |
| 1477 | unsigned long flags) |
| 1478 | { |
| 1479 | pci_addr_t bus_addr = 0; |
| 1480 | int ret; |
| 1481 | |
| 1482 | /* |
| 1483 | * if PCI_REGION_MEM is set we do a two pass search with preference |
| 1484 | * on matches that don't have PCI_REGION_SYS_MEMORY set |
| 1485 | */ |
| 1486 | if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { |
| 1487 | ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, |
| 1488 | PCI_REGION_SYS_MEMORY, &bus_addr); |
| 1489 | if (!ret) |
| 1490 | return bus_addr; |
| 1491 | } |
| 1492 | |
| 1493 | ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr); |
| 1494 | |
| 1495 | if (ret) |
| 1496 | puts("pci_hose_phys_to_bus: invalid physical address\n"); |
| 1497 | |
| 1498 | return bus_addr; |
| 1499 | } |
| 1500 | |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1501 | static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1502 | struct pci_child_plat *pdata) |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1503 | { |
| 1504 | phys_addr_t addr = 0; |
| 1505 | |
| 1506 | /* |
| 1507 | * In the case of a Virtual Function device using BAR |
| 1508 | * base and size, add offset for VFn BAR(1, 2, 3...n) |
| 1509 | */ |
| 1510 | if (pdata->is_virtfn) { |
| 1511 | size_t sz; |
| 1512 | u32 ea_entry; |
| 1513 | |
| 1514 | /* MaxOffset, 1st DW */ |
| 1515 | dm_pci_read_config32(dev, ea_off + 8, &ea_entry); |
| 1516 | sz = ea_entry & PCI_EA_FIELD_MASK; |
| 1517 | /* Fill up lower 2 bits */ |
| 1518 | sz |= (~PCI_EA_FIELD_MASK); |
| 1519 | |
| 1520 | if (ea_entry & PCI_EA_IS_64) { |
| 1521 | /* MaxOffset 2nd DW */ |
| 1522 | dm_pci_read_config32(dev, ea_off + 16, &ea_entry); |
| 1523 | sz |= ((u64)ea_entry) << 32; |
| 1524 | } |
| 1525 | |
| 1526 | addr = (pdata->virtid - 1) * (sz + 1); |
| 1527 | } |
| 1528 | |
| 1529 | return addr; |
| 1530 | } |
| 1531 | |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1532 | static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1533 | int ea_off, struct pci_child_plat *pdata) |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1534 | { |
| 1535 | int ea_cnt, i, entry_size; |
| 1536 | int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2; |
| 1537 | u32 ea_entry; |
| 1538 | phys_addr_t addr; |
| 1539 | |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1540 | if (IS_ENABLED(CONFIG_PCI_SRIOV)) { |
| 1541 | /* |
| 1542 | * In the case of a Virtual Function device, device is |
| 1543 | * Physical function, so pdata will point to required VF |
| 1544 | * specific data. |
| 1545 | */ |
| 1546 | if (pdata->is_virtfn) |
| 1547 | bar_id += PCI_EA_BEI_VF_BAR0; |
| 1548 | } |
| 1549 | |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1550 | /* EA capability structure header */ |
| 1551 | dm_pci_read_config32(dev, ea_off, &ea_entry); |
| 1552 | ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK; |
| 1553 | ea_off += PCI_EA_FIRST_ENT; |
| 1554 | |
| 1555 | for (i = 0; i < ea_cnt; i++, ea_off += entry_size) { |
| 1556 | /* Entry header */ |
| 1557 | dm_pci_read_config32(dev, ea_off, &ea_entry); |
| 1558 | entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2; |
| 1559 | |
| 1560 | if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id) |
| 1561 | continue; |
| 1562 | |
| 1563 | /* Base address, 1st DW */ |
| 1564 | dm_pci_read_config32(dev, ea_off + 4, &ea_entry); |
| 1565 | addr = ea_entry & PCI_EA_FIELD_MASK; |
| 1566 | if (ea_entry & PCI_EA_IS_64) { |
| 1567 | /* Base address, 2nd DW, skip over 4B MaxOffset */ |
| 1568 | dm_pci_read_config32(dev, ea_off + 12, &ea_entry); |
| 1569 | addr |= ((u64)ea_entry) << 32; |
| 1570 | } |
| 1571 | |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1572 | if (IS_ENABLED(CONFIG_PCI_SRIOV)) |
| 1573 | addr += dm_pci_map_ea_virt(dev, ea_off, pdata); |
| 1574 | |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1575 | /* size ignored for now */ |
Suneel Garapati | b3699a1 | 2019-10-19 16:44:35 -0700 | [diff] [blame] | 1576 | return map_physmem(addr, 0, flags); |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1582 | void *dm_pci_map_bar(struct udevice *dev, int bar, int flags) |
| 1583 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1584 | struct pci_child_plat *pdata = dev_get_parent_plat(dev); |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1585 | struct udevice *udev = dev; |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1586 | pci_addr_t pci_bus_addr; |
| 1587 | u32 bar_response; |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1588 | int ea_off; |
| 1589 | |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1590 | if (IS_ENABLED(CONFIG_PCI_SRIOV)) { |
| 1591 | /* |
| 1592 | * In case of Virtual Function devices, use PF udevice |
| 1593 | * as EA capability is defined in Physical Function |
| 1594 | */ |
| 1595 | if (pdata->is_virtfn) |
| 1596 | udev = pdata->pfdev; |
| 1597 | } |
| 1598 | |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1599 | /* |
| 1600 | * if the function supports Enhanced Allocation use that instead of |
| 1601 | * BARs |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1602 | * Incase of virtual functions, pdata will help read VF BEI |
| 1603 | * and EA entry size. |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1604 | */ |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1605 | ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA); |
Alex Marginean | 0b143d8 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1606 | if (ea_off) |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1607 | return dm_pci_map_ea_bar(udev, bar, flags, ea_off, pdata); |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1608 | |
| 1609 | /* read BAR address */ |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1610 | dm_pci_read_config32(udev, bar, &bar_response); |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1611 | pci_bus_addr = (pci_addr_t)(bar_response & ~0xf); |
| 1612 | |
| 1613 | /* |
| 1614 | * Pass "0" as the length argument to pci_bus_to_virt. The arg |
Suneel Garapati | b3699a1 | 2019-10-19 16:44:35 -0700 | [diff] [blame] | 1615 | * isn't actually used on any platform because U-Boot assumes a static |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1616 | * linear mapping. In the future, this could read the BAR size |
| 1617 | * and pass that as the size if needed. |
| 1618 | */ |
Suneel Garapati | 51eeae9 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1619 | return dm_pci_bus_to_virt(udev, pci_bus_addr, flags, 0, MAP_NOCACHE); |
Simon Glass | 21d1fe7 | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
Bin Meng | a8c5f8d | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1622 | static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap) |
Bin Meng | dac01fd | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1623 | { |
Bin Meng | dac01fd | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1624 | int ttl = PCI_FIND_CAP_TTL; |
| 1625 | u8 id; |
| 1626 | u16 ent; |
Bin Meng | dac01fd | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1627 | |
| 1628 | dm_pci_read_config8(dev, pos, &pos); |
Bin Meng | a8c5f8d | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1629 | |
Bin Meng | dac01fd | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1630 | while (ttl--) { |
| 1631 | if (pos < PCI_STD_HEADER_SIZEOF) |
| 1632 | break; |
| 1633 | pos &= ~3; |
| 1634 | dm_pci_read_config16(dev, pos, &ent); |
| 1635 | |
| 1636 | id = ent & 0xff; |
| 1637 | if (id == 0xff) |
| 1638 | break; |
| 1639 | if (id == cap) |
| 1640 | return pos; |
| 1641 | pos = (ent >> 8); |
| 1642 | } |
| 1643 | |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
Bin Meng | a8c5f8d | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1647 | int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap) |
| 1648 | { |
| 1649 | return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT, |
| 1650 | cap); |
| 1651 | } |
| 1652 | |
| 1653 | int dm_pci_find_capability(struct udevice *dev, int cap) |
| 1654 | { |
| 1655 | u16 status; |
| 1656 | u8 header_type; |
| 1657 | u8 pos; |
| 1658 | |
| 1659 | dm_pci_read_config16(dev, PCI_STATUS, &status); |
| 1660 | if (!(status & PCI_STATUS_CAP_LIST)) |
| 1661 | return 0; |
| 1662 | |
| 1663 | dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); |
| 1664 | if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) |
| 1665 | pos = PCI_CB_CAPABILITY_LIST; |
| 1666 | else |
| 1667 | pos = PCI_CAPABILITY_LIST; |
| 1668 | |
| 1669 | return _dm_pci_find_next_capability(dev, pos, cap); |
| 1670 | } |
| 1671 | |
| 1672 | int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap) |
Bin Meng | dac01fd | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1673 | { |
| 1674 | u32 header; |
| 1675 | int ttl; |
| 1676 | int pos = PCI_CFG_SPACE_SIZE; |
| 1677 | |
| 1678 | /* minimum 8 bytes per capability */ |
| 1679 | ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; |
| 1680 | |
Bin Meng | a8c5f8d | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1681 | if (start) |
| 1682 | pos = start; |
| 1683 | |
Bin Meng | dac01fd | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1684 | dm_pci_read_config32(dev, pos, &header); |
| 1685 | /* |
| 1686 | * If we have no capabilities, this is indicated by cap ID, |
| 1687 | * cap version and next pointer all being 0. |
| 1688 | */ |
| 1689 | if (header == 0) |
| 1690 | return 0; |
| 1691 | |
| 1692 | while (ttl--) { |
| 1693 | if (PCI_EXT_CAP_ID(header) == cap) |
| 1694 | return pos; |
| 1695 | |
| 1696 | pos = PCI_EXT_CAP_NEXT(header); |
| 1697 | if (pos < PCI_CFG_SPACE_SIZE) |
| 1698 | break; |
| 1699 | |
| 1700 | dm_pci_read_config32(dev, pos, &header); |
| 1701 | } |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
Bin Meng | a8c5f8d | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1706 | int dm_pci_find_ext_capability(struct udevice *dev, int cap) |
| 1707 | { |
| 1708 | return dm_pci_find_next_ext_capability(dev, 0, cap); |
| 1709 | } |
| 1710 | |
Alex Marginean | b8e1f82 | 2019-06-07 11:24:25 +0300 | [diff] [blame] | 1711 | int dm_pci_flr(struct udevice *dev) |
| 1712 | { |
| 1713 | int pcie_off; |
| 1714 | u32 cap; |
| 1715 | |
| 1716 | /* look for PCI Express Capability */ |
| 1717 | pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 1718 | if (!pcie_off) |
| 1719 | return -ENOENT; |
| 1720 | |
| 1721 | /* check FLR capability */ |
| 1722 | dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap); |
| 1723 | if (!(cap & PCI_EXP_DEVCAP_FLR)) |
| 1724 | return -ENOENT; |
| 1725 | |
| 1726 | dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0, |
| 1727 | PCI_EXP_DEVCTL_BCR_FLR); |
| 1728 | |
| 1729 | /* wait 100ms, per PCI spec */ |
| 1730 | mdelay(100); |
| 1731 | |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
Suneel Garapati | b8852dc | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1735 | #if defined(CONFIG_PCI_SRIOV) |
| 1736 | int pci_sriov_init(struct udevice *pdev, int vf_en) |
| 1737 | { |
| 1738 | u16 vendor, device; |
| 1739 | struct udevice *bus; |
| 1740 | struct udevice *dev; |
| 1741 | pci_dev_t bdf; |
| 1742 | u16 ctrl; |
| 1743 | u16 num_vfs; |
| 1744 | u16 total_vf; |
| 1745 | u16 vf_offset; |
| 1746 | u16 vf_stride; |
| 1747 | int vf, ret; |
| 1748 | int pos; |
| 1749 | |
| 1750 | pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); |
| 1751 | if (!pos) { |
| 1752 | debug("Error: SRIOV capability not found\n"); |
| 1753 | return -ENOENT; |
| 1754 | } |
| 1755 | |
| 1756 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl); |
| 1757 | |
| 1758 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf); |
| 1759 | if (vf_en > total_vf) |
| 1760 | vf_en = total_vf; |
| 1761 | dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en); |
| 1762 | |
| 1763 | ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; |
| 1764 | dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl); |
| 1765 | |
| 1766 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs); |
| 1767 | if (num_vfs > vf_en) |
| 1768 | num_vfs = vf_en; |
| 1769 | |
| 1770 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset); |
| 1771 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride); |
| 1772 | |
| 1773 | dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor); |
| 1774 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device); |
| 1775 | |
| 1776 | bdf = dm_pci_get_bdf(pdev); |
| 1777 | |
| 1778 | pci_get_bus(PCI_BUS(bdf), &bus); |
| 1779 | |
| 1780 | if (!bus) |
| 1781 | return -ENODEV; |
| 1782 | |
| 1783 | bdf += PCI_BDF(0, 0, vf_offset); |
| 1784 | |
| 1785 | for (vf = 0; vf < num_vfs; vf++) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1786 | struct pci_child_plat *pplat; |
Suneel Garapati | b8852dc | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1787 | ulong class; |
| 1788 | |
| 1789 | pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE, |
| 1790 | &class, PCI_SIZE_16); |
| 1791 | |
| 1792 | debug("%s: bus %d/%s: found VF %x:%x\n", __func__, |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1793 | dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf)); |
Suneel Garapati | b8852dc | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1794 | |
| 1795 | /* Find this device in the device tree */ |
| 1796 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev); |
| 1797 | |
| 1798 | if (ret == -ENODEV) { |
| 1799 | struct pci_device_id find_id; |
| 1800 | |
| 1801 | memset(&find_id, '\0', sizeof(find_id)); |
| 1802 | find_id.vendor = vendor; |
| 1803 | find_id.device = device; |
| 1804 | find_id.class = class; |
| 1805 | |
| 1806 | ret = pci_find_and_bind_driver(bus, &find_id, |
| 1807 | bdf, &dev); |
| 1808 | |
| 1809 | if (ret) |
| 1810 | return ret; |
| 1811 | } |
| 1812 | |
| 1813 | /* Update the platform data */ |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1814 | pplat = dev_get_parent_plat(dev); |
Suneel Garapati | b8852dc | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1815 | pplat->devfn = PCI_MASK_BUS(bdf); |
| 1816 | pplat->vendor = vendor; |
| 1817 | pplat->device = device; |
| 1818 | pplat->class = class; |
| 1819 | pplat->is_virtfn = true; |
| 1820 | pplat->pfdev = pdev; |
| 1821 | pplat->virtid = vf * vf_stride + vf_offset; |
| 1822 | |
| 1823 | debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n", |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1824 | __func__, dev_seq(dev), dev->name, PCI_DEV(bdf), |
Suneel Garapati | b8852dc | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1825 | PCI_FUNC(bdf), vendor, device, class, pplat->virtid); |
| 1826 | bdf += PCI_BDF(0, 0, vf_stride); |
| 1827 | } |
| 1828 | |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | int pci_sriov_get_totalvfs(struct udevice *pdev) |
| 1833 | { |
| 1834 | u16 total_vf; |
| 1835 | int pos; |
| 1836 | |
| 1837 | pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); |
| 1838 | if (!pos) { |
| 1839 | debug("Error: SRIOV capability not found\n"); |
| 1840 | return -ENOENT; |
| 1841 | } |
| 1842 | |
| 1843 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf); |
| 1844 | |
| 1845 | return total_vf; |
| 1846 | } |
| 1847 | #endif /* SRIOV */ |
| 1848 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1849 | UCLASS_DRIVER(pci) = { |
| 1850 | .id = UCLASS_PCI, |
| 1851 | .name = "pci", |
Simon Glass | 42f3663 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1852 | .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ, |
Simon Glass | 9119548 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 1853 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1854 | .pre_probe = pci_uclass_pre_probe, |
| 1855 | .post_probe = pci_uclass_post_probe, |
| 1856 | .child_post_bind = pci_uclass_child_post_bind, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1857 | .per_device_auto = sizeof(struct pci_controller), |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1858 | .per_child_plat_auto = sizeof(struct pci_child_plat), |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1859 | }; |
| 1860 | |
| 1861 | static const struct dm_pci_ops pci_bridge_ops = { |
| 1862 | .read_config = pci_bridge_read_config, |
| 1863 | .write_config = pci_bridge_write_config, |
| 1864 | }; |
| 1865 | |
| 1866 | static const struct udevice_id pci_bridge_ids[] = { |
| 1867 | { .compatible = "pci-bridge" }, |
| 1868 | { } |
| 1869 | }; |
| 1870 | |
| 1871 | U_BOOT_DRIVER(pci_bridge_drv) = { |
| 1872 | .name = "pci_bridge_drv", |
| 1873 | .id = UCLASS_PCI, |
| 1874 | .of_match = pci_bridge_ids, |
| 1875 | .ops = &pci_bridge_ops, |
| 1876 | }; |
| 1877 | |
| 1878 | UCLASS_DRIVER(pci_generic) = { |
| 1879 | .id = UCLASS_PCI_GENERIC, |
| 1880 | .name = "pci_generic", |
| 1881 | }; |
| 1882 | |
| 1883 | static const struct udevice_id pci_generic_ids[] = { |
| 1884 | { .compatible = "pci-generic" }, |
| 1885 | { } |
| 1886 | }; |
| 1887 | |
| 1888 | U_BOOT_DRIVER(pci_generic_drv) = { |
| 1889 | .name = "pci_generic_drv", |
| 1890 | .id = UCLASS_PCI_GENERIC, |
| 1891 | .of_match = pci_generic_ids, |
| 1892 | }; |
Stephen Warren | e578b92 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1893 | |
Ovidiu Panait | b9f6d0f | 2020-11-28 10:43:12 +0200 | [diff] [blame] | 1894 | int pci_init(void) |
Stephen Warren | e578b92 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1895 | { |
| 1896 | struct udevice *bus; |
| 1897 | |
| 1898 | /* |
| 1899 | * Enumerate all known controller devices. Enumeration has the side- |
| 1900 | * effect of probing them, so PCIe devices will be enumerated too. |
| 1901 | */ |
Marek BehĂșn | 60ee609 | 2019-05-21 12:04:31 +0200 | [diff] [blame] | 1902 | for (uclass_first_device_check(UCLASS_PCI, &bus); |
Stephen Warren | e578b92 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1903 | bus; |
Marek BehĂșn | 60ee609 | 2019-05-21 12:04:31 +0200 | [diff] [blame] | 1904 | uclass_next_device_check(&bus)) { |
Stephen Warren | e578b92 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1905 | ; |
| 1906 | } |
Ovidiu Panait | b9f6d0f | 2020-11-28 10:43:12 +0200 | [diff] [blame] | 1907 | |
| 1908 | return 0; |
Stephen Warren | e578b92 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1909 | } |