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