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