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