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); |
Simon Glass | f3005fb | 2020-04-08 16:57:26 -0600 | [diff] [blame] | 542 | if (dev_read_bool(dev, "pci,no-autoconfig")) |
| 543 | continue; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 544 | ret = dm_pciauto_config_device(dev); |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 545 | if (ret < 0) |
| 546 | return ret; |
| 547 | max_bus = ret; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 548 | sub_bus = max(sub_bus, max_bus); |
Bin Meng | a070578 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 549 | |
| 550 | pplat = dev_get_parent_platdata(dev); |
| 551 | if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8)) |
| 552 | set_vga_bridge_bits(dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 553 | } |
| 554 | debug("%s: done\n", __func__); |
| 555 | |
| 556 | return sub_bus; |
| 557 | } |
| 558 | |
Tuomas Tynkkynen | 8cce4cf | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 559 | int pci_generic_mmap_write_config( |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 560 | const struct udevice *bus, |
| 561 | int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset, |
| 562 | void **addrp), |
Tuomas Tynkkynen | 8cce4cf | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 563 | pci_dev_t bdf, |
| 564 | uint offset, |
| 565 | ulong value, |
| 566 | enum pci_size_t size) |
| 567 | { |
| 568 | void *address; |
| 569 | |
| 570 | if (addr_f(bus, bdf, offset, &address) < 0) |
| 571 | return 0; |
| 572 | |
| 573 | switch (size) { |
| 574 | case PCI_SIZE_8: |
| 575 | writeb(value, address); |
| 576 | return 0; |
| 577 | case PCI_SIZE_16: |
| 578 | writew(value, address); |
| 579 | return 0; |
| 580 | case PCI_SIZE_32: |
| 581 | writel(value, address); |
| 582 | return 0; |
| 583 | default: |
| 584 | return -EINVAL; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | int pci_generic_mmap_read_config( |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 589 | const struct udevice *bus, |
| 590 | int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset, |
| 591 | void **addrp), |
Tuomas Tynkkynen | 8cce4cf | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 592 | pci_dev_t bdf, |
| 593 | uint offset, |
| 594 | ulong *valuep, |
| 595 | enum pci_size_t size) |
| 596 | { |
| 597 | void *address; |
| 598 | |
| 599 | if (addr_f(bus, bdf, offset, &address) < 0) { |
| 600 | *valuep = pci_get_ff(size); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | switch (size) { |
| 605 | case PCI_SIZE_8: |
| 606 | *valuep = readb(address); |
| 607 | return 0; |
| 608 | case PCI_SIZE_16: |
| 609 | *valuep = readw(address); |
| 610 | return 0; |
| 611 | case PCI_SIZE_32: |
| 612 | *valuep = readl(address); |
| 613 | return 0; |
| 614 | default: |
| 615 | return -EINVAL; |
| 616 | } |
| 617 | } |
| 618 | |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 619 | int dm_pci_hose_probe_bus(struct udevice *bus) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 620 | { |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 621 | int sub_bus; |
| 622 | int ret; |
| 623 | |
| 624 | debug("%s\n", __func__); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 625 | |
| 626 | sub_bus = pci_get_bus_max() + 1; |
| 627 | debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 628 | dm_pciauto_prescan_setup_bridge(bus, sub_bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 629 | |
| 630 | ret = device_probe(bus); |
| 631 | if (ret) { |
Simon Glass | 3b02d84 | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 632 | debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 633 | ret); |
| 634 | return ret; |
| 635 | } |
| 636 | if (sub_bus != bus->seq) { |
| 637 | printf("%s: Internal error, bus '%s' got seq %d, expected %d\n", |
| 638 | __func__, bus->name, bus->seq, sub_bus); |
| 639 | return -EPIPE; |
| 640 | } |
| 641 | sub_bus = pci_get_bus_max(); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 642 | dm_pciauto_postscan_setup_bridge(bus, sub_bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 643 | |
| 644 | return sub_bus; |
| 645 | } |
| 646 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 647 | /** |
| 648 | * pci_match_one_device - Tell if a PCI device structure has a matching |
| 649 | * PCI device id structure |
| 650 | * @id: single PCI device id structure to match |
Hou Zhiqiang | d19d061 | 2017-03-22 16:07:24 +0800 | [diff] [blame] | 651 | * @find: the PCI device id structure to match against |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 652 | * |
Hou Zhiqiang | d19d061 | 2017-03-22 16:07:24 +0800 | [diff] [blame] | 653 | * Returns true if the finding pci_device_id structure matched or false if |
| 654 | * there is no match. |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 655 | */ |
| 656 | static bool pci_match_one_id(const struct pci_device_id *id, |
| 657 | const struct pci_device_id *find) |
| 658 | { |
| 659 | if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) && |
| 660 | (id->device == PCI_ANY_ID || id->device == find->device) && |
| 661 | (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) && |
| 662 | (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) && |
| 663 | !((id->class ^ find->class) & id->class_mask)) |
| 664 | return true; |
| 665 | |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * pci_find_and_bind_driver() - Find and bind the right PCI driver |
| 671 | * |
| 672 | * This only looks at certain fields in the descriptor. |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 673 | * |
| 674 | * @parent: Parent bus |
| 675 | * @find_id: Specification of the driver to find |
| 676 | * @bdf: Bus/device/function addreess - see PCI_BDF() |
| 677 | * @devp: Returns a pointer to the device created |
| 678 | * @return 0 if OK, -EPERM if the device is not needed before relocation and |
| 679 | * therefore was not created, other -ve value on error |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 680 | */ |
| 681 | static int pci_find_and_bind_driver(struct udevice *parent, |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 682 | struct pci_device_id *find_id, |
| 683 | pci_dev_t bdf, struct udevice **devp) |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 684 | { |
| 685 | struct pci_driver_entry *start, *entry; |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 686 | ofnode node = ofnode_null(); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 687 | const char *drv; |
| 688 | int n_ents; |
| 689 | int ret; |
| 690 | char name[30], *str; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 691 | bool bridge; |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 692 | |
| 693 | *devp = NULL; |
| 694 | |
| 695 | debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__, |
| 696 | find_id->vendor, find_id->device); |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 697 | |
| 698 | /* Determine optional OF node */ |
| 699 | pci_dev_find_ofnode(parent, bdf, &node); |
| 700 | |
Michael Walle | 2e21f37 | 2019-12-01 17:45:18 +0100 | [diff] [blame] | 701 | if (ofnode_valid(node) && !ofnode_is_available(node)) { |
| 702 | debug("%s: Ignoring disabled device\n", __func__); |
| 703 | return -EPERM; |
| 704 | } |
| 705 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 706 | start = ll_entry_start(struct pci_driver_entry, pci_driver_entry); |
| 707 | n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry); |
| 708 | for (entry = start; entry != start + n_ents; entry++) { |
| 709 | const struct pci_device_id *id; |
| 710 | struct udevice *dev; |
| 711 | const struct driver *drv; |
| 712 | |
| 713 | for (id = entry->match; |
| 714 | id->vendor || id->subvendor || id->class_mask; |
| 715 | id++) { |
| 716 | if (!pci_match_one_id(id, find_id)) |
| 717 | continue; |
| 718 | |
| 719 | drv = entry->driver; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * In the pre-relocation phase, we only bind devices |
| 723 | * whose driver has the DM_FLAG_PRE_RELOC set, to save |
| 724 | * precious memory space as on some platforms as that |
| 725 | * space is pretty limited (ie: using Cache As RAM). |
| 726 | */ |
| 727 | if (!(gd->flags & GD_FLG_RELOC) && |
| 728 | !(drv->flags & DM_FLAG_PRE_RELOC)) |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 729 | return -EPERM; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 730 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 731 | /* |
| 732 | * We could pass the descriptor to the driver as |
| 733 | * platdata (instead of NULL) and allow its bind() |
| 734 | * method to return -ENOENT if it doesn't support this |
| 735 | * device. That way we could continue the search to |
| 736 | * find another driver. For now this doesn't seem |
| 737 | * necesssary, so just bind the first match. |
| 738 | */ |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 739 | ret = device_bind_ofnode(parent, drv, drv->name, NULL, |
| 740 | node, &dev); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 741 | if (ret) |
| 742 | goto error; |
| 743 | debug("%s: Match found: %s\n", __func__, drv->name); |
Bin Meng | a8d2780 | 2018-08-03 01:14:44 -0700 | [diff] [blame] | 744 | dev->driver_data = id->driver_data; |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 745 | *devp = dev; |
| 746 | return 0; |
| 747 | } |
| 748 | } |
| 749 | |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 750 | bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI; |
| 751 | /* |
| 752 | * In the pre-relocation phase, we only bind bridge devices to save |
| 753 | * precious memory space as on some platforms as that space is pretty |
| 754 | * limited (ie: using Cache As RAM). |
| 755 | */ |
| 756 | if (!(gd->flags & GD_FLG_RELOC) && !bridge) |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 757 | return -EPERM; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 758 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 759 | /* Bind a generic driver so that the device can be used */ |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 760 | sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf), |
| 761 | PCI_FUNC(bdf)); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 762 | str = strdup(name); |
| 763 | if (!str) |
| 764 | return -ENOMEM; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 765 | drv = bridge ? "pci_bridge_drv" : "pci_generic_drv"; |
| 766 | |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 767 | ret = device_bind_driver_to_node(parent, drv, str, node, devp); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 768 | if (ret) { |
Simon Glass | 3b02d84 | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 769 | 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] | 770 | free(str); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 771 | return ret; |
| 772 | } |
| 773 | debug("%s: No match found: bound generic driver instead\n", __func__); |
| 774 | |
| 775 | return 0; |
| 776 | |
| 777 | error: |
| 778 | debug("%s: No match found: error %d\n", __func__, ret); |
| 779 | return ret; |
| 780 | } |
| 781 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 782 | int pci_bind_bus_devices(struct udevice *bus) |
| 783 | { |
| 784 | ulong vendor, device; |
| 785 | ulong header_type; |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 786 | pci_dev_t bdf, end; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 787 | bool found_multi; |
| 788 | int ret; |
| 789 | |
| 790 | found_multi = false; |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 791 | end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1, |
| 792 | PCI_MAX_PCI_FUNCTIONS - 1); |
Yoshinori Sato | 1e3bce2 | 2016-04-25 15:41:01 +0900 | [diff] [blame] | 793 | for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end; |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 794 | bdf += PCI_BDF(0, 0, 1)) { |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 795 | struct pci_child_platdata *pplat; |
| 796 | struct udevice *dev; |
| 797 | ulong class; |
| 798 | |
Bin Meng | 20bdc1e | 2018-08-03 01:14:37 -0700 | [diff] [blame] | 799 | if (!PCI_FUNC(bdf)) |
| 800 | found_multi = false; |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 801 | if (PCI_FUNC(bdf) && !found_multi) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 802 | continue; |
Hou Zhiqiang | fb862b05 | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 803 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 804 | /* Check only the first access, we don't expect problems */ |
Hou Zhiqiang | fb862b05 | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 805 | ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor, |
| 806 | PCI_SIZE_16); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 807 | if (ret) |
| 808 | goto error; |
Hou Zhiqiang | fb862b05 | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 809 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 810 | if (vendor == 0xffff || vendor == 0x0000) |
| 811 | continue; |
| 812 | |
Hou Zhiqiang | fb862b05 | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 813 | pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE, |
| 814 | &header_type, PCI_SIZE_8); |
| 815 | |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 816 | if (!PCI_FUNC(bdf)) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 817 | found_multi = header_type & 0x80; |
| 818 | |
Simon Glass | 25916d6 | 2019-09-25 08:56:12 -0600 | [diff] [blame] | 819 | debug("%s: bus %d/%s: found device %x, function %d", __func__, |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 820 | bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 821 | pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 822 | PCI_SIZE_16); |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 823 | pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class, |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 824 | PCI_SIZE_32); |
| 825 | class >>= 8; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 826 | |
| 827 | /* Find this device in the device tree */ |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 828 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev); |
Simon Glass | 25916d6 | 2019-09-25 08:56:12 -0600 | [diff] [blame] | 829 | debug(": find ret=%d\n", ret); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 830 | |
Simon Glass | 413ebdb | 2015-11-29 13:18:09 -0700 | [diff] [blame] | 831 | /* If nothing in the device tree, bind a device */ |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 832 | if (ret == -ENODEV) { |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 833 | struct pci_device_id find_id; |
| 834 | ulong val; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 835 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 836 | memset(&find_id, '\0', sizeof(find_id)); |
| 837 | find_id.vendor = vendor; |
| 838 | find_id.device = device; |
| 839 | find_id.class = class; |
| 840 | if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) { |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 841 | pci_bus_read_config(bus, bdf, |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 842 | PCI_SUBSYSTEM_VENDOR_ID, |
| 843 | &val, PCI_SIZE_32); |
| 844 | find_id.subvendor = val & 0xffff; |
| 845 | find_id.subdevice = val >> 16; |
| 846 | } |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 847 | ret = pci_find_and_bind_driver(bus, &find_id, bdf, |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 848 | &dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 849 | } |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 850 | if (ret == -EPERM) |
| 851 | continue; |
| 852 | else if (ret) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 853 | return ret; |
| 854 | |
| 855 | /* Update the platform data */ |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 856 | pplat = dev_get_parent_platdata(dev); |
| 857 | pplat->devfn = PCI_MASK_BUS(bdf); |
| 858 | pplat->vendor = vendor; |
| 859 | pplat->device = device; |
| 860 | pplat->class = class; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | error: |
| 865 | printf("Cannot read bus configuration: %d\n", ret); |
| 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 870 | static void decode_regions(struct pci_controller *hose, ofnode parent_node, |
| 871 | ofnode node) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 872 | { |
| 873 | int pci_addr_cells, addr_cells, size_cells; |
| 874 | int cells_per_record; |
| 875 | const u32 *prop; |
| 876 | int len; |
| 877 | int i; |
| 878 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 879 | prop = ofnode_get_property(node, "ranges", &len); |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 880 | if (!prop) { |
| 881 | debug("%s: Cannot decode regions\n", __func__); |
| 882 | return; |
| 883 | } |
| 884 | |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 885 | pci_addr_cells = ofnode_read_simple_addr_cells(node); |
| 886 | addr_cells = ofnode_read_simple_addr_cells(parent_node); |
| 887 | size_cells = ofnode_read_simple_size_cells(node); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 888 | |
| 889 | /* PCI addresses are always 3-cells */ |
| 890 | len /= sizeof(u32); |
| 891 | cells_per_record = pci_addr_cells + addr_cells + size_cells; |
| 892 | hose->region_count = 0; |
| 893 | debug("%s: len=%d, cells_per_record=%d\n", __func__, len, |
| 894 | cells_per_record); |
| 895 | for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) { |
| 896 | u64 pci_addr, addr, size; |
| 897 | int space_code; |
| 898 | u32 flags; |
| 899 | int type; |
Simon Glass | 7efc9ba | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 900 | int pos; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 901 | |
| 902 | if (len < cells_per_record) |
| 903 | break; |
| 904 | flags = fdt32_to_cpu(prop[0]); |
| 905 | space_code = (flags >> 24) & 3; |
| 906 | pci_addr = fdtdec_get_number(prop + 1, 2); |
| 907 | prop += pci_addr_cells; |
| 908 | addr = fdtdec_get_number(prop, addr_cells); |
| 909 | prop += addr_cells; |
| 910 | size = fdtdec_get_number(prop, size_cells); |
| 911 | prop += size_cells; |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 912 | debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n", |
| 913 | __func__, hose->region_count, pci_addr, addr, size, space_code); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 914 | if (space_code & 2) { |
| 915 | type = flags & (1U << 30) ? PCI_REGION_PREFETCH : |
| 916 | PCI_REGION_MEM; |
| 917 | } else if (space_code & 1) { |
| 918 | type = PCI_REGION_IO; |
| 919 | } else { |
| 920 | continue; |
| 921 | } |
Tuomas Tynkkynen | c307e17 | 2018-05-14 18:47:50 +0300 | [diff] [blame] | 922 | |
| 923 | if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) && |
| 924 | type == PCI_REGION_MEM && upper_32_bits(pci_addr)) { |
| 925 | debug(" - beyond the 32-bit boundary, ignoring\n"); |
| 926 | continue; |
| 927 | } |
| 928 | |
Simon Glass | 7efc9ba | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 929 | pos = -1; |
| 930 | for (i = 0; i < hose->region_count; i++) { |
| 931 | if (hose->regions[i].flags == type) |
| 932 | pos = i; |
| 933 | } |
| 934 | if (pos == -1) |
| 935 | pos = hose->region_count++; |
| 936 | debug(" - type=%d, pos=%d\n", type, pos); |
| 937 | pci_set_region(hose->regions + pos, pci_addr, addr, size, type); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /* Add a region for our local memory */ |
Bernhard Messerklinger | 9c5df38 | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 941 | #ifdef CONFIG_NR_DRAM_BANKS |
| 942 | bd_t *bd = gd->bd; |
| 943 | |
Bin Meng | ae0bdde | 2018-03-27 00:46:05 -0700 | [diff] [blame] | 944 | if (!bd) |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 945 | return; |
Bin Meng | ae0bdde | 2018-03-27 00:46:05 -0700 | [diff] [blame] | 946 | |
Bernhard Messerklinger | 9c5df38 | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 947 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) { |
Thierry Reding | adf0d99 | 2019-03-15 16:32:32 +0100 | [diff] [blame] | 948 | if (hose->region_count == MAX_PCI_REGIONS) { |
| 949 | pr_err("maximum number of regions parsed, aborting\n"); |
| 950 | break; |
| 951 | } |
| 952 | |
Bernhard Messerklinger | 9c5df38 | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 953 | if (bd->bi_dram[i].size) { |
| 954 | pci_set_region(hose->regions + hose->region_count++, |
| 955 | bd->bi_dram[i].start, |
| 956 | bd->bi_dram[i].start, |
| 957 | bd->bi_dram[i].size, |
| 958 | PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
| 959 | } |
| 960 | } |
| 961 | #else |
| 962 | phys_addr_t base = 0, size; |
| 963 | |
Simon Glass | 91de6c5 | 2015-11-19 20:26:57 -0700 | [diff] [blame] | 964 | size = gd->ram_size; |
| 965 | #ifdef CONFIG_SYS_SDRAM_BASE |
| 966 | base = CONFIG_SYS_SDRAM_BASE; |
| 967 | #endif |
| 968 | if (gd->pci_ram_top && gd->pci_ram_top < base + size) |
| 969 | size = gd->pci_ram_top - base; |
Bin Meng | 6d53207 | 2018-03-27 00:46:06 -0700 | [diff] [blame] | 970 | if (size) |
| 971 | pci_set_region(hose->regions + hose->region_count++, base, |
| 972 | base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
Bernhard Messerklinger | 9c5df38 | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 973 | #endif |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 974 | |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 975 | return; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static int pci_uclass_pre_probe(struct udevice *bus) |
| 979 | { |
| 980 | struct pci_controller *hose; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 981 | |
| 982 | debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name, |
| 983 | bus->parent->name); |
| 984 | hose = bus->uclass_priv; |
| 985 | |
| 986 | /* For bridges, use the top-level PCI controller */ |
Paul Burton | e3b106d | 2016-09-08 07:47:32 +0100 | [diff] [blame] | 987 | if (!device_is_on_pci_bus(bus)) { |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 988 | hose->ctlr = bus; |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 989 | decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus)); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 990 | } else { |
| 991 | struct pci_controller *parent_hose; |
| 992 | |
| 993 | parent_hose = dev_get_uclass_priv(bus->parent); |
| 994 | hose->ctlr = parent_hose->bus; |
| 995 | } |
| 996 | hose->bus = bus; |
| 997 | hose->first_busno = bus->seq; |
| 998 | hose->last_busno = bus->seq; |
Simon Glass | 68e35a7 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 999 | hose->skip_auto_config_until_reloc = |
| 1000 | dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc"); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1001 | |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | static int pci_uclass_post_probe(struct udevice *bus) |
| 1006 | { |
Simon Glass | 68e35a7 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1007 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1008 | int ret; |
| 1009 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1010 | debug("%s: probing bus %d\n", __func__, bus->seq); |
| 1011 | ret = pci_bind_bus_devices(bus); |
| 1012 | if (ret) |
| 1013 | return ret; |
| 1014 | |
Simon Glass | bd165e7 | 2020-04-26 09:12:56 -0600 | [diff] [blame] | 1015 | if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() && |
Simon Glass | 68e35a7 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1016 | (!hose->skip_auto_config_until_reloc || |
| 1017 | (gd->flags & GD_FLG_RELOC))) { |
| 1018 | ret = pci_auto_config_devices(bus); |
| 1019 | if (ret < 0) |
| 1020 | return log_msg_ret("pci auto-config", ret); |
| 1021 | } |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1022 | |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1023 | #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) |
| 1024 | /* |
| 1025 | * Per Intel FSP specification, we should call FSP notify API to |
| 1026 | * inform FSP that PCI enumeration has been done so that FSP will |
| 1027 | * do any necessary initialization as required by the chipset's |
| 1028 | * BIOS Writer's Guide (BWG). |
| 1029 | * |
| 1030 | * Unfortunately we have to put this call here as with driver model, |
| 1031 | * the enumeration is all done on a lazy basis as needed, so until |
| 1032 | * something is touched on PCI it won't happen. |
| 1033 | * |
| 1034 | * Note we only call this 1) after U-Boot is relocated, and 2) |
| 1035 | * root bus has finished probing. |
| 1036 | */ |
Simon Glass | bd165e7 | 2020-04-26 09:12:56 -0600 | [diff] [blame] | 1037 | if ((gd->flags & GD_FLG_RELOC) && bus->seq == 0 && ll_boot_init()) { |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1038 | ret = fsp_init_phase_pci(); |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1039 | if (ret) |
| 1040 | return ret; |
| 1041 | } |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1042 | #endif |
| 1043 | |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1044 | return 0; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | static int pci_uclass_child_post_bind(struct udevice *dev) |
| 1048 | { |
| 1049 | struct pci_child_platdata *pplat; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1050 | |
Simon Glass | 89d8323 | 2017-05-18 20:09:51 -0600 | [diff] [blame] | 1051 | if (!dev_of_valid(dev)) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1054 | pplat = dev_get_parent_platdata(dev); |
Bin Meng | 00d808e | 2018-08-03 01:14:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* Extract vendor id and device id if available */ |
| 1057 | ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device); |
| 1058 | |
| 1059 | /* Extract the devfn from fdt_pci_addr */ |
Stefan Roese | a74eb55 | 2019-01-25 11:52:42 +0100 | [diff] [blame] | 1060 | pplat->devfn = pci_get_devfn(dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 1065 | 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] | 1066 | uint offset, ulong *valuep, |
| 1067 | enum pci_size_t size) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1068 | { |
| 1069 | struct pci_controller *hose = bus->uclass_priv; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1070 | |
| 1071 | return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size); |
| 1072 | } |
| 1073 | |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 1074 | static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf, |
| 1075 | uint offset, ulong value, |
| 1076 | enum pci_size_t size) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1077 | { |
| 1078 | struct pci_controller *hose = bus->uclass_priv; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1079 | |
| 1080 | return pci_bus_write_config(hose->ctlr, bdf, offset, value, size); |
| 1081 | } |
| 1082 | |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1083 | static int skip_to_next_device(struct udevice *bus, struct udevice **devp) |
| 1084 | { |
| 1085 | struct udevice *dev; |
| 1086 | int ret = 0; |
| 1087 | |
| 1088 | /* |
| 1089 | * Scan through all the PCI controllers. On x86 there will only be one |
| 1090 | * but that is not necessarily true on other hardware. |
| 1091 | */ |
| 1092 | do { |
| 1093 | device_find_first_child(bus, &dev); |
| 1094 | if (dev) { |
| 1095 | *devp = dev; |
| 1096 | return 0; |
| 1097 | } |
| 1098 | ret = uclass_next_device(&bus); |
| 1099 | if (ret) |
| 1100 | return ret; |
| 1101 | } while (bus); |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | int pci_find_next_device(struct udevice **devp) |
| 1107 | { |
| 1108 | struct udevice *child = *devp; |
| 1109 | struct udevice *bus = child->parent; |
| 1110 | int ret; |
| 1111 | |
| 1112 | /* First try all the siblings */ |
| 1113 | *devp = NULL; |
| 1114 | while (child) { |
| 1115 | device_find_next_child(&child); |
| 1116 | if (child) { |
| 1117 | *devp = child; |
| 1118 | return 0; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* We ran out of siblings. Try the next bus */ |
| 1123 | ret = uclass_next_device(&bus); |
| 1124 | if (ret) |
| 1125 | return ret; |
| 1126 | |
| 1127 | return bus ? skip_to_next_device(bus, devp) : 0; |
| 1128 | } |
| 1129 | |
| 1130 | int pci_find_first_device(struct udevice **devp) |
| 1131 | { |
| 1132 | struct udevice *bus; |
| 1133 | int ret; |
| 1134 | |
| 1135 | *devp = NULL; |
| 1136 | ret = uclass_first_device(UCLASS_PCI, &bus); |
| 1137 | if (ret) |
| 1138 | return ret; |
| 1139 | |
| 1140 | return skip_to_next_device(bus, devp); |
| 1141 | } |
| 1142 | |
Simon Glass | 27a733f | 2015-11-19 20:26:59 -0700 | [diff] [blame] | 1143 | ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size) |
| 1144 | { |
| 1145 | switch (size) { |
| 1146 | case PCI_SIZE_8: |
| 1147 | return (value >> ((offset & 3) * 8)) & 0xff; |
| 1148 | case PCI_SIZE_16: |
| 1149 | return (value >> ((offset & 2) * 8)) & 0xffff; |
| 1150 | default: |
| 1151 | return value; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | ulong pci_conv_size_to_32(ulong old, ulong value, uint offset, |
| 1156 | enum pci_size_t size) |
| 1157 | { |
| 1158 | uint off_mask; |
| 1159 | uint val_mask, shift; |
| 1160 | ulong ldata, mask; |
| 1161 | |
| 1162 | switch (size) { |
| 1163 | case PCI_SIZE_8: |
| 1164 | off_mask = 3; |
| 1165 | val_mask = 0xff; |
| 1166 | break; |
| 1167 | case PCI_SIZE_16: |
| 1168 | off_mask = 2; |
| 1169 | val_mask = 0xffff; |
| 1170 | break; |
| 1171 | default: |
| 1172 | return value; |
| 1173 | } |
| 1174 | shift = (offset & off_mask) * 8; |
| 1175 | ldata = (value & val_mask) << shift; |
| 1176 | mask = val_mask << shift; |
| 1177 | value = (old & ~mask) | ldata; |
| 1178 | |
| 1179 | return value; |
| 1180 | } |
| 1181 | |
Simon Glass | dcdc012 | 2015-11-19 20:27:01 -0700 | [diff] [blame] | 1182 | int pci_get_regions(struct udevice *dev, struct pci_region **iop, |
| 1183 | struct pci_region **memp, struct pci_region **prefp) |
| 1184 | { |
| 1185 | struct udevice *bus = pci_get_controller(dev); |
| 1186 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
| 1187 | int i; |
| 1188 | |
| 1189 | *iop = NULL; |
| 1190 | *memp = NULL; |
| 1191 | *prefp = NULL; |
| 1192 | for (i = 0; i < hose->region_count; i++) { |
| 1193 | switch (hose->regions[i].flags) { |
| 1194 | case PCI_REGION_IO: |
| 1195 | if (!*iop || (*iop)->size < hose->regions[i].size) |
| 1196 | *iop = hose->regions + i; |
| 1197 | break; |
| 1198 | case PCI_REGION_MEM: |
| 1199 | if (!*memp || (*memp)->size < hose->regions[i].size) |
| 1200 | *memp = hose->regions + i; |
| 1201 | break; |
| 1202 | case (PCI_REGION_MEM | PCI_REGION_PREFETCH): |
| 1203 | if (!*prefp || (*prefp)->size < hose->regions[i].size) |
| 1204 | *prefp = hose->regions + i; |
| 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL); |
| 1210 | } |
| 1211 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 1212 | u32 dm_pci_read_bar32(const struct udevice *dev, int barnum) |
Simon Glass | 3452cb1 | 2015-11-29 13:17:53 -0700 | [diff] [blame] | 1213 | { |
| 1214 | u32 addr; |
| 1215 | int bar; |
| 1216 | |
| 1217 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 1218 | dm_pci_read_config32(dev, bar, &addr); |
Simon Glass | 71fafd1 | 2020-04-09 10:27:36 -0600 | [diff] [blame] | 1219 | |
| 1220 | /* |
| 1221 | * If we get an invalid address, return this so that comparisons with |
| 1222 | * FDT_ADDR_T_NONE work correctly |
| 1223 | */ |
| 1224 | if (addr == 0xffffffff) |
| 1225 | return addr; |
| 1226 | else if (addr & PCI_BASE_ADDRESS_SPACE_IO) |
Simon Glass | 3452cb1 | 2015-11-29 13:17:53 -0700 | [diff] [blame] | 1227 | return addr & PCI_BASE_ADDRESS_IO_MASK; |
| 1228 | else |
| 1229 | return addr & PCI_BASE_ADDRESS_MEM_MASK; |
| 1230 | } |
| 1231 | |
Simon Glass | e2b6b56 | 2016-01-18 20:19:15 -0700 | [diff] [blame] | 1232 | void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr) |
| 1233 | { |
| 1234 | int bar; |
| 1235 | |
| 1236 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 1237 | dm_pci_write_config32(dev, bar, addr); |
| 1238 | } |
| 1239 | |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1240 | static int _dm_pci_bus_to_phys(struct udevice *ctlr, |
| 1241 | pci_addr_t bus_addr, unsigned long flags, |
| 1242 | unsigned long skip_mask, phys_addr_t *pa) |
| 1243 | { |
| 1244 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 1245 | struct pci_region *res; |
| 1246 | int i; |
| 1247 | |
Christian Gmeiner | 7241f80 | 2018-06-10 06:25:06 -0700 | [diff] [blame] | 1248 | if (hose->region_count == 0) { |
| 1249 | *pa = bus_addr; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1253 | for (i = 0; i < hose->region_count; i++) { |
| 1254 | res = &hose->regions[i]; |
| 1255 | |
| 1256 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 1257 | continue; |
| 1258 | |
| 1259 | if (res->flags & skip_mask) |
| 1260 | continue; |
| 1261 | |
| 1262 | if (bus_addr >= res->bus_start && |
| 1263 | (bus_addr - res->bus_start) < res->size) { |
| 1264 | *pa = (bus_addr - res->bus_start + res->phys_start); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | return 1; |
| 1270 | } |
| 1271 | |
| 1272 | phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, |
| 1273 | unsigned long flags) |
| 1274 | { |
| 1275 | phys_addr_t phys_addr = 0; |
| 1276 | struct udevice *ctlr; |
| 1277 | int ret; |
| 1278 | |
| 1279 | /* The root controller has the region information */ |
| 1280 | ctlr = pci_get_controller(dev); |
| 1281 | |
| 1282 | /* |
| 1283 | * if PCI_REGION_MEM is set we do a two pass search with preference |
| 1284 | * on matches that don't have PCI_REGION_SYS_MEMORY set |
| 1285 | */ |
| 1286 | if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { |
| 1287 | ret = _dm_pci_bus_to_phys(ctlr, bus_addr, |
| 1288 | flags, PCI_REGION_SYS_MEMORY, |
| 1289 | &phys_addr); |
| 1290 | if (!ret) |
| 1291 | return phys_addr; |
| 1292 | } |
| 1293 | |
| 1294 | ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr); |
| 1295 | |
| 1296 | if (ret) |
| 1297 | puts("pci_hose_bus_to_phys: invalid physical address\n"); |
| 1298 | |
| 1299 | return phys_addr; |
| 1300 | } |
| 1301 | |
| 1302 | int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, |
| 1303 | unsigned long flags, unsigned long skip_mask, |
| 1304 | pci_addr_t *ba) |
| 1305 | { |
| 1306 | struct pci_region *res; |
| 1307 | struct udevice *ctlr; |
| 1308 | pci_addr_t bus_addr; |
| 1309 | int i; |
| 1310 | struct pci_controller *hose; |
| 1311 | |
| 1312 | /* The root controller has the region information */ |
| 1313 | ctlr = pci_get_controller(dev); |
| 1314 | hose = dev_get_uclass_priv(ctlr); |
| 1315 | |
Christian Gmeiner | 7241f80 | 2018-06-10 06:25:06 -0700 | [diff] [blame] | 1316 | if (hose->region_count == 0) { |
| 1317 | *ba = phys_addr; |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1321 | for (i = 0; i < hose->region_count; i++) { |
| 1322 | res = &hose->regions[i]; |
| 1323 | |
| 1324 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 1325 | continue; |
| 1326 | |
| 1327 | if (res->flags & skip_mask) |
| 1328 | continue; |
| 1329 | |
| 1330 | bus_addr = phys_addr - res->phys_start + res->bus_start; |
| 1331 | |
| 1332 | if (bus_addr >= res->bus_start && |
| 1333 | (bus_addr - res->bus_start) < res->size) { |
| 1334 | *ba = bus_addr; |
| 1335 | return 0; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | return 1; |
| 1340 | } |
| 1341 | |
| 1342 | pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, |
| 1343 | unsigned long flags) |
| 1344 | { |
| 1345 | pci_addr_t bus_addr = 0; |
| 1346 | int ret; |
| 1347 | |
| 1348 | /* |
| 1349 | * if PCI_REGION_MEM is set we do a two pass search with preference |
| 1350 | * on matches that don't have PCI_REGION_SYS_MEMORY set |
| 1351 | */ |
| 1352 | if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { |
| 1353 | ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, |
| 1354 | PCI_REGION_SYS_MEMORY, &bus_addr); |
| 1355 | if (!ret) |
| 1356 | return bus_addr; |
| 1357 | } |
| 1358 | |
| 1359 | ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr); |
| 1360 | |
| 1361 | if (ret) |
| 1362 | puts("pci_hose_phys_to_bus: invalid physical address\n"); |
| 1363 | |
| 1364 | return bus_addr; |
| 1365 | } |
| 1366 | |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1367 | static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags, |
| 1368 | int ea_off) |
| 1369 | { |
| 1370 | int ea_cnt, i, entry_size; |
| 1371 | int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2; |
| 1372 | u32 ea_entry; |
| 1373 | phys_addr_t addr; |
| 1374 | |
| 1375 | /* EA capability structure header */ |
| 1376 | dm_pci_read_config32(dev, ea_off, &ea_entry); |
| 1377 | ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK; |
| 1378 | ea_off += PCI_EA_FIRST_ENT; |
| 1379 | |
| 1380 | for (i = 0; i < ea_cnt; i++, ea_off += entry_size) { |
| 1381 | /* Entry header */ |
| 1382 | dm_pci_read_config32(dev, ea_off, &ea_entry); |
| 1383 | entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2; |
| 1384 | |
| 1385 | if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id) |
| 1386 | continue; |
| 1387 | |
| 1388 | /* Base address, 1st DW */ |
| 1389 | dm_pci_read_config32(dev, ea_off + 4, &ea_entry); |
| 1390 | addr = ea_entry & PCI_EA_FIELD_MASK; |
| 1391 | if (ea_entry & PCI_EA_IS_64) { |
| 1392 | /* Base address, 2nd DW, skip over 4B MaxOffset */ |
| 1393 | dm_pci_read_config32(dev, ea_off + 12, &ea_entry); |
| 1394 | addr |= ((u64)ea_entry) << 32; |
| 1395 | } |
| 1396 | |
| 1397 | /* size ignored for now */ |
| 1398 | return map_physmem(addr, flags, 0); |
| 1399 | } |
| 1400 | |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1404 | void *dm_pci_map_bar(struct udevice *dev, int bar, int flags) |
| 1405 | { |
| 1406 | pci_addr_t pci_bus_addr; |
| 1407 | u32 bar_response; |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1408 | int ea_off; |
| 1409 | |
| 1410 | /* |
| 1411 | * if the function supports Enhanced Allocation use that instead of |
| 1412 | * BARs |
| 1413 | */ |
| 1414 | ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA); |
| 1415 | if (ea_off) |
| 1416 | return dm_pci_map_ea_bar(dev, bar, flags, ea_off); |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1417 | |
| 1418 | /* read BAR address */ |
| 1419 | dm_pci_read_config32(dev, bar, &bar_response); |
| 1420 | pci_bus_addr = (pci_addr_t)(bar_response & ~0xf); |
| 1421 | |
| 1422 | /* |
| 1423 | * Pass "0" as the length argument to pci_bus_to_virt. The arg |
| 1424 | * isn't actualy used on any platform because u-boot assumes a static |
| 1425 | * linear mapping. In the future, this could read the BAR size |
| 1426 | * and pass that as the size if needed. |
| 1427 | */ |
| 1428 | return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE); |
| 1429 | } |
| 1430 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1431 | 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] | 1432 | { |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1433 | int ttl = PCI_FIND_CAP_TTL; |
| 1434 | u8 id; |
| 1435 | u16 ent; |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1436 | |
| 1437 | dm_pci_read_config8(dev, pos, &pos); |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1438 | |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1439 | while (ttl--) { |
| 1440 | if (pos < PCI_STD_HEADER_SIZEOF) |
| 1441 | break; |
| 1442 | pos &= ~3; |
| 1443 | dm_pci_read_config16(dev, pos, &ent); |
| 1444 | |
| 1445 | id = ent & 0xff; |
| 1446 | if (id == 0xff) |
| 1447 | break; |
| 1448 | if (id == cap) |
| 1449 | return pos; |
| 1450 | pos = (ent >> 8); |
| 1451 | } |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1456 | int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap) |
| 1457 | { |
| 1458 | return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT, |
| 1459 | cap); |
| 1460 | } |
| 1461 | |
| 1462 | int dm_pci_find_capability(struct udevice *dev, int cap) |
| 1463 | { |
| 1464 | u16 status; |
| 1465 | u8 header_type; |
| 1466 | u8 pos; |
| 1467 | |
| 1468 | dm_pci_read_config16(dev, PCI_STATUS, &status); |
| 1469 | if (!(status & PCI_STATUS_CAP_LIST)) |
| 1470 | return 0; |
| 1471 | |
| 1472 | dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); |
| 1473 | if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) |
| 1474 | pos = PCI_CB_CAPABILITY_LIST; |
| 1475 | else |
| 1476 | pos = PCI_CAPABILITY_LIST; |
| 1477 | |
| 1478 | return _dm_pci_find_next_capability(dev, pos, cap); |
| 1479 | } |
| 1480 | |
| 1481 | 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] | 1482 | { |
| 1483 | u32 header; |
| 1484 | int ttl; |
| 1485 | int pos = PCI_CFG_SPACE_SIZE; |
| 1486 | |
| 1487 | /* minimum 8 bytes per capability */ |
| 1488 | ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; |
| 1489 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1490 | if (start) |
| 1491 | pos = start; |
| 1492 | |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1493 | dm_pci_read_config32(dev, pos, &header); |
| 1494 | /* |
| 1495 | * If we have no capabilities, this is indicated by cap ID, |
| 1496 | * cap version and next pointer all being 0. |
| 1497 | */ |
| 1498 | if (header == 0) |
| 1499 | return 0; |
| 1500 | |
| 1501 | while (ttl--) { |
| 1502 | if (PCI_EXT_CAP_ID(header) == cap) |
| 1503 | return pos; |
| 1504 | |
| 1505 | pos = PCI_EXT_CAP_NEXT(header); |
| 1506 | if (pos < PCI_CFG_SPACE_SIZE) |
| 1507 | break; |
| 1508 | |
| 1509 | dm_pci_read_config32(dev, pos, &header); |
| 1510 | } |
| 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1515 | int dm_pci_find_ext_capability(struct udevice *dev, int cap) |
| 1516 | { |
| 1517 | return dm_pci_find_next_ext_capability(dev, 0, cap); |
| 1518 | } |
| 1519 | |
Alex Marginean | 09467d3 | 2019-06-07 11:24:25 +0300 | [diff] [blame] | 1520 | int dm_pci_flr(struct udevice *dev) |
| 1521 | { |
| 1522 | int pcie_off; |
| 1523 | u32 cap; |
| 1524 | |
| 1525 | /* look for PCI Express Capability */ |
| 1526 | pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 1527 | if (!pcie_off) |
| 1528 | return -ENOENT; |
| 1529 | |
| 1530 | /* check FLR capability */ |
| 1531 | dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap); |
| 1532 | if (!(cap & PCI_EXP_DEVCAP_FLR)) |
| 1533 | return -ENOENT; |
| 1534 | |
| 1535 | dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0, |
| 1536 | PCI_EXP_DEVCTL_BCR_FLR); |
| 1537 | |
| 1538 | /* wait 100ms, per PCI spec */ |
| 1539 | mdelay(100); |
| 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1544 | UCLASS_DRIVER(pci) = { |
| 1545 | .id = UCLASS_PCI, |
| 1546 | .name = "pci", |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 1547 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 1548 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1549 | .pre_probe = pci_uclass_pre_probe, |
| 1550 | .post_probe = pci_uclass_post_probe, |
| 1551 | .child_post_bind = pci_uclass_child_post_bind, |
| 1552 | .per_device_auto_alloc_size = sizeof(struct pci_controller), |
| 1553 | .per_child_platdata_auto_alloc_size = |
| 1554 | sizeof(struct pci_child_platdata), |
| 1555 | }; |
| 1556 | |
| 1557 | static const struct dm_pci_ops pci_bridge_ops = { |
| 1558 | .read_config = pci_bridge_read_config, |
| 1559 | .write_config = pci_bridge_write_config, |
| 1560 | }; |
| 1561 | |
| 1562 | static const struct udevice_id pci_bridge_ids[] = { |
| 1563 | { .compatible = "pci-bridge" }, |
| 1564 | { } |
| 1565 | }; |
| 1566 | |
| 1567 | U_BOOT_DRIVER(pci_bridge_drv) = { |
| 1568 | .name = "pci_bridge_drv", |
| 1569 | .id = UCLASS_PCI, |
| 1570 | .of_match = pci_bridge_ids, |
| 1571 | .ops = &pci_bridge_ops, |
| 1572 | }; |
| 1573 | |
| 1574 | UCLASS_DRIVER(pci_generic) = { |
| 1575 | .id = UCLASS_PCI_GENERIC, |
| 1576 | .name = "pci_generic", |
| 1577 | }; |
| 1578 | |
| 1579 | static const struct udevice_id pci_generic_ids[] = { |
| 1580 | { .compatible = "pci-generic" }, |
| 1581 | { } |
| 1582 | }; |
| 1583 | |
| 1584 | U_BOOT_DRIVER(pci_generic_drv) = { |
| 1585 | .name = "pci_generic_drv", |
| 1586 | .id = UCLASS_PCI_GENERIC, |
| 1587 | .of_match = pci_generic_ids, |
| 1588 | }; |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1589 | |
| 1590 | void pci_init(void) |
| 1591 | { |
| 1592 | struct udevice *bus; |
| 1593 | |
| 1594 | /* |
| 1595 | * Enumerate all known controller devices. Enumeration has the side- |
| 1596 | * effect of probing them, so PCIe devices will be enumerated too. |
| 1597 | */ |
Marek BehĂșn | 5df208d | 2019-05-21 12:04:31 +0200 | [diff] [blame] | 1598 | for (uclass_first_device_check(UCLASS_PCI, &bus); |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1599 | bus; |
Marek BehĂșn | 5df208d | 2019-05-21 12:04:31 +0200 | [diff] [blame] | 1600 | uclass_next_device_check(&bus)) { |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1601 | ; |
| 1602 | } |
| 1603 | } |