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