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