Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <fdtdec.h> |
| 12 | #include <inttypes.h> |
| 13 | #include <pci.h> |
| 14 | #include <dm/lists.h> |
| 15 | #include <dm/root.h> |
| 16 | #include <dm/device-internal.h> |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 17 | #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) |
| 18 | #include <asm/fsp/fsp_support.h> |
| 19 | #endif |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame^] | 20 | #include "pci_internal.h" |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 24 | static int pci_get_bus(int busnum, struct udevice **busp) |
| 25 | { |
| 26 | int ret; |
| 27 | |
| 28 | ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); |
| 29 | |
| 30 | /* Since buses may not be numbered yet try a little harder with bus 0 */ |
| 31 | if (ret == -ENODEV) { |
| 32 | ret = uclass_first_device(UCLASS_PCI, busp); |
| 33 | if (ret) |
| 34 | return ret; |
| 35 | else if (!*busp) |
| 36 | return -ENODEV; |
| 37 | ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); |
| 38 | } |
| 39 | |
| 40 | return ret; |
| 41 | } |
| 42 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 43 | struct pci_controller *pci_bus_to_hose(int busnum) |
| 44 | { |
| 45 | struct udevice *bus; |
| 46 | int ret; |
| 47 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 48 | ret = pci_get_bus(busnum, &bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 49 | if (ret) { |
| 50 | debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret); |
| 51 | return NULL; |
| 52 | } |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 53 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 54 | return dev_get_uclass_priv(bus); |
| 55 | } |
| 56 | |
Simon Glass | 9f60fb0 | 2015-11-19 20:27:00 -0700 | [diff] [blame] | 57 | struct udevice *pci_get_controller(struct udevice *dev) |
| 58 | { |
| 59 | while (device_is_on_pci_bus(dev)) |
| 60 | dev = dev->parent; |
| 61 | |
| 62 | return dev; |
| 63 | } |
| 64 | |
Simon Glass | 21ccce1 | 2015-11-29 13:17:47 -0700 | [diff] [blame] | 65 | pci_dev_t dm_pci_get_bdf(struct udevice *dev) |
Simon Glass | 4b515e4 | 2015-07-06 16:47:46 -0600 | [diff] [blame] | 66 | { |
| 67 | struct pci_child_platdata *pplat = dev_get_parent_platdata(dev); |
| 68 | struct udevice *bus = dev->parent; |
| 69 | |
| 70 | return PCI_ADD_BUS(bus->seq, pplat->devfn); |
| 71 | } |
| 72 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 73 | /** |
| 74 | * pci_get_bus_max() - returns the bus number of the last active bus |
| 75 | * |
| 76 | * @return last bus number, or -1 if no active buses |
| 77 | */ |
| 78 | static int pci_get_bus_max(void) |
| 79 | { |
| 80 | struct udevice *bus; |
| 81 | struct uclass *uc; |
| 82 | int ret = -1; |
| 83 | |
| 84 | ret = uclass_get(UCLASS_PCI, &uc); |
| 85 | uclass_foreach_dev(bus, uc) { |
| 86 | if (bus->seq > ret) |
| 87 | ret = bus->seq; |
| 88 | } |
| 89 | |
| 90 | debug("%s: ret=%d\n", __func__, ret); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | int pci_last_busno(void) |
| 96 | { |
Bin Meng | 069155c | 2015-10-01 00:36:01 -0700 | [diff] [blame] | 97 | return pci_get_bus_max(); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int pci_get_ff(enum pci_size_t size) |
| 101 | { |
| 102 | switch (size) { |
| 103 | case PCI_SIZE_8: |
| 104 | return 0xff; |
| 105 | case PCI_SIZE_16: |
| 106 | return 0xffff; |
| 107 | default: |
| 108 | return 0xffffffff; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn, |
| 113 | struct udevice **devp) |
| 114 | { |
| 115 | struct udevice *dev; |
| 116 | |
| 117 | for (device_find_first_child(bus, &dev); |
| 118 | dev; |
| 119 | device_find_next_child(&dev)) { |
| 120 | struct pci_child_platdata *pplat; |
| 121 | |
| 122 | pplat = dev_get_parent_platdata(dev); |
| 123 | if (pplat && pplat->devfn == find_devfn) { |
| 124 | *devp = dev; |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return -ENODEV; |
| 130 | } |
| 131 | |
Simon Glass | f3f1fae | 2015-11-29 13:17:48 -0700 | [diff] [blame] | 132 | 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] | 133 | { |
| 134 | struct udevice *bus; |
| 135 | int ret; |
| 136 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 137 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 138 | if (ret) |
| 139 | return ret; |
| 140 | return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp); |
| 141 | } |
| 142 | |
| 143 | static int pci_device_matches_ids(struct udevice *dev, |
| 144 | struct pci_device_id *ids) |
| 145 | { |
| 146 | struct pci_child_platdata *pplat; |
| 147 | int i; |
| 148 | |
| 149 | pplat = dev_get_parent_platdata(dev); |
| 150 | if (!pplat) |
| 151 | return -EINVAL; |
| 152 | for (i = 0; ids[i].vendor != 0; i++) { |
| 153 | if (pplat->vendor == ids[i].vendor && |
| 154 | pplat->device == ids[i].device) |
| 155 | return i; |
| 156 | } |
| 157 | |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids, |
| 162 | int *indexp, struct udevice **devp) |
| 163 | { |
| 164 | struct udevice *dev; |
| 165 | |
| 166 | /* Scan all devices on this bus */ |
| 167 | for (device_find_first_child(bus, &dev); |
| 168 | dev; |
| 169 | device_find_next_child(&dev)) { |
| 170 | if (pci_device_matches_ids(dev, ids) >= 0) { |
| 171 | if ((*indexp)-- <= 0) { |
| 172 | *devp = dev; |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return -ENODEV; |
| 179 | } |
| 180 | |
| 181 | int pci_find_device_id(struct pci_device_id *ids, int index, |
| 182 | struct udevice **devp) |
| 183 | { |
| 184 | struct udevice *bus; |
| 185 | |
| 186 | /* Scan all known buses */ |
| 187 | for (uclass_first_device(UCLASS_PCI, &bus); |
| 188 | bus; |
| 189 | uclass_next_device(&bus)) { |
| 190 | if (!pci_bus_find_devices(bus, ids, &index, devp)) |
| 191 | return 0; |
| 192 | } |
| 193 | *devp = NULL; |
| 194 | |
| 195 | return -ENODEV; |
| 196 | } |
| 197 | |
| 198 | int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset, |
| 199 | unsigned long value, enum pci_size_t size) |
| 200 | { |
| 201 | struct dm_pci_ops *ops; |
| 202 | |
| 203 | ops = pci_get_ops(bus); |
| 204 | if (!ops->write_config) |
| 205 | return -ENOSYS; |
| 206 | return ops->write_config(bus, bdf, offset, value, size); |
| 207 | } |
| 208 | |
| 209 | int pci_write_config(pci_dev_t bdf, int offset, unsigned long value, |
| 210 | enum pci_size_t size) |
| 211 | { |
| 212 | struct udevice *bus; |
| 213 | int ret; |
| 214 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 215 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 216 | if (ret) |
| 217 | return ret; |
| 218 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 219 | return pci_bus_write_config(bus, bdf, offset, value, size); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 222 | int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value, |
| 223 | enum pci_size_t size) |
| 224 | { |
| 225 | struct udevice *bus; |
| 226 | |
Bin Meng | 1e0f226 | 2015-09-11 03:24:34 -0700 | [diff] [blame] | 227 | for (bus = dev; device_is_on_pci_bus(bus);) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 228 | bus = bus->parent; |
Simon Glass | 21ccce1 | 2015-11-29 13:17:47 -0700 | [diff] [blame] | 229 | return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value, |
| 230 | size); |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 234 | int pci_write_config32(pci_dev_t bdf, int offset, u32 value) |
| 235 | { |
| 236 | return pci_write_config(bdf, offset, value, PCI_SIZE_32); |
| 237 | } |
| 238 | |
| 239 | int pci_write_config16(pci_dev_t bdf, int offset, u16 value) |
| 240 | { |
| 241 | return pci_write_config(bdf, offset, value, PCI_SIZE_16); |
| 242 | } |
| 243 | |
| 244 | int pci_write_config8(pci_dev_t bdf, int offset, u8 value) |
| 245 | { |
| 246 | return pci_write_config(bdf, offset, value, PCI_SIZE_8); |
| 247 | } |
| 248 | |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 249 | int dm_pci_write_config8(struct udevice *dev, int offset, u8 value) |
| 250 | { |
| 251 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_8); |
| 252 | } |
| 253 | |
| 254 | int dm_pci_write_config16(struct udevice *dev, int offset, u16 value) |
| 255 | { |
| 256 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_16); |
| 257 | } |
| 258 | |
| 259 | int dm_pci_write_config32(struct udevice *dev, int offset, u32 value) |
| 260 | { |
| 261 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_32); |
| 262 | } |
| 263 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 264 | int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset, |
| 265 | unsigned long *valuep, enum pci_size_t size) |
| 266 | { |
| 267 | struct dm_pci_ops *ops; |
| 268 | |
| 269 | ops = pci_get_ops(bus); |
| 270 | if (!ops->read_config) |
| 271 | return -ENOSYS; |
| 272 | return ops->read_config(bus, bdf, offset, valuep, size); |
| 273 | } |
| 274 | |
| 275 | int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep, |
| 276 | enum pci_size_t size) |
| 277 | { |
| 278 | struct udevice *bus; |
| 279 | int ret; |
| 280 | |
Simon Glass | 983c6ba2 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 281 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 282 | if (ret) |
| 283 | return ret; |
| 284 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 285 | return pci_bus_read_config(bus, bdf, offset, valuep, size); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 288 | int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep, |
| 289 | enum pci_size_t size) |
| 290 | { |
| 291 | struct udevice *bus; |
| 292 | |
Bin Meng | 1e0f226 | 2015-09-11 03:24:34 -0700 | [diff] [blame] | 293 | for (bus = dev; device_is_on_pci_bus(bus);) |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 294 | bus = bus->parent; |
Simon Glass | 21ccce1 | 2015-11-29 13:17:47 -0700 | [diff] [blame] | 295 | 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] | 296 | size); |
| 297 | } |
| 298 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 299 | int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep) |
| 300 | { |
| 301 | unsigned long value; |
| 302 | int ret; |
| 303 | |
| 304 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | *valuep = value; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep) |
| 313 | { |
| 314 | unsigned long value; |
| 315 | int ret; |
| 316 | |
| 317 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16); |
| 318 | if (ret) |
| 319 | return ret; |
| 320 | *valuep = value; |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep) |
| 326 | { |
| 327 | unsigned long value; |
| 328 | int ret; |
| 329 | |
| 330 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | *valuep = value; |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Simon Glass | 66afb4e | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 338 | int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep) |
| 339 | { |
| 340 | unsigned long value; |
| 341 | int ret; |
| 342 | |
| 343 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | *valuep = value; |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep) |
| 352 | { |
| 353 | unsigned long value; |
| 354 | int ret; |
| 355 | |
| 356 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16); |
| 357 | if (ret) |
| 358 | return ret; |
| 359 | *valuep = value; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep) |
| 365 | { |
| 366 | unsigned long value; |
| 367 | int ret; |
| 368 | |
| 369 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32); |
| 370 | if (ret) |
| 371 | return ret; |
| 372 | *valuep = value; |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Bin Meng | bbbcb52 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 377 | static void set_vga_bridge_bits(struct udevice *dev) |
| 378 | { |
| 379 | struct udevice *parent = dev->parent; |
| 380 | u16 bc; |
| 381 | |
| 382 | while (parent->seq != 0) { |
| 383 | dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc); |
| 384 | bc |= PCI_BRIDGE_CTL_VGA; |
| 385 | dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc); |
| 386 | parent = parent->parent; |
| 387 | } |
| 388 | } |
| 389 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 390 | int pci_auto_config_devices(struct udevice *bus) |
| 391 | { |
| 392 | struct pci_controller *hose = bus->uclass_priv; |
Bin Meng | bbbcb52 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 393 | struct pci_child_platdata *pplat; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 394 | unsigned int sub_bus; |
| 395 | struct udevice *dev; |
| 396 | int ret; |
| 397 | |
| 398 | sub_bus = bus->seq; |
| 399 | debug("%s: start\n", __func__); |
| 400 | pciauto_config_init(hose); |
| 401 | for (ret = device_find_first_child(bus, &dev); |
| 402 | !ret && dev; |
| 403 | ret = device_find_next_child(&dev)) { |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 404 | unsigned int max_bus; |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 405 | int ret; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 406 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 407 | debug("%s: device %s\n", __func__, dev->name); |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame^] | 408 | ret = dm_pciauto_config_device(dev); |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 409 | if (ret < 0) |
| 410 | return ret; |
| 411 | max_bus = ret; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 412 | sub_bus = max(sub_bus, max_bus); |
Bin Meng | bbbcb52 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 413 | |
| 414 | pplat = dev_get_parent_platdata(dev); |
| 415 | if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8)) |
| 416 | set_vga_bridge_bits(dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 417 | } |
| 418 | debug("%s: done\n", __func__); |
| 419 | |
| 420 | return sub_bus; |
| 421 | } |
| 422 | |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame^] | 423 | int dm_pci_hose_probe_bus(struct udevice *bus) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 424 | { |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 425 | int sub_bus; |
| 426 | int ret; |
| 427 | |
| 428 | debug("%s\n", __func__); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 429 | |
| 430 | sub_bus = pci_get_bus_max() + 1; |
| 431 | debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name); |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame^] | 432 | dm_pciauto_prescan_setup_bridge(bus, sub_bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 433 | |
| 434 | ret = device_probe(bus); |
| 435 | if (ret) { |
Simon Glass | 3129ace | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 436 | debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 437 | ret); |
| 438 | return ret; |
| 439 | } |
| 440 | if (sub_bus != bus->seq) { |
| 441 | printf("%s: Internal error, bus '%s' got seq %d, expected %d\n", |
| 442 | __func__, bus->name, bus->seq, sub_bus); |
| 443 | return -EPIPE; |
| 444 | } |
| 445 | sub_bus = pci_get_bus_max(); |
Simon Glass | 5e23b8b | 2015-11-29 13:17:49 -0700 | [diff] [blame^] | 446 | dm_pciauto_postscan_setup_bridge(bus, sub_bus); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 447 | |
| 448 | return sub_bus; |
| 449 | } |
| 450 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 451 | /** |
| 452 | * pci_match_one_device - Tell if a PCI device structure has a matching |
| 453 | * PCI device id structure |
| 454 | * @id: single PCI device id structure to match |
| 455 | * @dev: the PCI device structure to match against |
| 456 | * |
| 457 | * Returns the matching pci_device_id structure or %NULL if there is no match. |
| 458 | */ |
| 459 | static bool pci_match_one_id(const struct pci_device_id *id, |
| 460 | const struct pci_device_id *find) |
| 461 | { |
| 462 | if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) && |
| 463 | (id->device == PCI_ANY_ID || id->device == find->device) && |
| 464 | (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) && |
| 465 | (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) && |
| 466 | !((id->class ^ find->class) & id->class_mask)) |
| 467 | return true; |
| 468 | |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * pci_find_and_bind_driver() - Find and bind the right PCI driver |
| 474 | * |
| 475 | * This only looks at certain fields in the descriptor. |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 476 | * |
| 477 | * @parent: Parent bus |
| 478 | * @find_id: Specification of the driver to find |
| 479 | * @bdf: Bus/device/function addreess - see PCI_BDF() |
| 480 | * @devp: Returns a pointer to the device created |
| 481 | * @return 0 if OK, -EPERM if the device is not needed before relocation and |
| 482 | * therefore was not created, other -ve value on error |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 483 | */ |
| 484 | static int pci_find_and_bind_driver(struct udevice *parent, |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 485 | struct pci_device_id *find_id, |
| 486 | pci_dev_t bdf, struct udevice **devp) |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 487 | { |
| 488 | struct pci_driver_entry *start, *entry; |
| 489 | const char *drv; |
| 490 | int n_ents; |
| 491 | int ret; |
| 492 | char name[30], *str; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 493 | bool bridge; |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 494 | |
| 495 | *devp = NULL; |
| 496 | |
| 497 | debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__, |
| 498 | find_id->vendor, find_id->device); |
| 499 | start = ll_entry_start(struct pci_driver_entry, pci_driver_entry); |
| 500 | n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry); |
| 501 | for (entry = start; entry != start + n_ents; entry++) { |
| 502 | const struct pci_device_id *id; |
| 503 | struct udevice *dev; |
| 504 | const struct driver *drv; |
| 505 | |
| 506 | for (id = entry->match; |
| 507 | id->vendor || id->subvendor || id->class_mask; |
| 508 | id++) { |
| 509 | if (!pci_match_one_id(id, find_id)) |
| 510 | continue; |
| 511 | |
| 512 | drv = entry->driver; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * In the pre-relocation phase, we only bind devices |
| 516 | * whose driver has the DM_FLAG_PRE_RELOC set, to save |
| 517 | * precious memory space as on some platforms as that |
| 518 | * space is pretty limited (ie: using Cache As RAM). |
| 519 | */ |
| 520 | if (!(gd->flags & GD_FLG_RELOC) && |
| 521 | !(drv->flags & DM_FLAG_PRE_RELOC)) |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 522 | return -EPERM; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 523 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 524 | /* |
| 525 | * We could pass the descriptor to the driver as |
| 526 | * platdata (instead of NULL) and allow its bind() |
| 527 | * method to return -ENOENT if it doesn't support this |
| 528 | * device. That way we could continue the search to |
| 529 | * find another driver. For now this doesn't seem |
| 530 | * necesssary, so just bind the first match. |
| 531 | */ |
| 532 | ret = device_bind(parent, drv, drv->name, NULL, -1, |
| 533 | &dev); |
| 534 | if (ret) |
| 535 | goto error; |
| 536 | debug("%s: Match found: %s\n", __func__, drv->name); |
| 537 | dev->driver_data = find_id->driver_data; |
| 538 | *devp = dev; |
| 539 | return 0; |
| 540 | } |
| 541 | } |
| 542 | |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 543 | bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI; |
| 544 | /* |
| 545 | * In the pre-relocation phase, we only bind bridge devices to save |
| 546 | * precious memory space as on some platforms as that space is pretty |
| 547 | * limited (ie: using Cache As RAM). |
| 548 | */ |
| 549 | if (!(gd->flags & GD_FLG_RELOC) && !bridge) |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 550 | return -EPERM; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 551 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 552 | /* Bind a generic driver so that the device can be used */ |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 553 | sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf), |
| 554 | PCI_FUNC(bdf)); |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 555 | str = strdup(name); |
| 556 | if (!str) |
| 557 | return -ENOMEM; |
Bin Meng | 08fc7b8 | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 558 | drv = bridge ? "pci_bridge_drv" : "pci_generic_drv"; |
| 559 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 560 | ret = device_bind_driver(parent, drv, str, devp); |
| 561 | if (ret) { |
Simon Glass | 3129ace | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 562 | debug("%s: Failed to bind generic driver: %d\n", __func__, ret); |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 563 | return ret; |
| 564 | } |
| 565 | debug("%s: No match found: bound generic driver instead\n", __func__); |
| 566 | |
| 567 | return 0; |
| 568 | |
| 569 | error: |
| 570 | debug("%s: No match found: error %d\n", __func__, ret); |
| 571 | return ret; |
| 572 | } |
| 573 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 574 | int pci_bind_bus_devices(struct udevice *bus) |
| 575 | { |
| 576 | ulong vendor, device; |
| 577 | ulong header_type; |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 578 | pci_dev_t bdf, end; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 579 | bool found_multi; |
| 580 | int ret; |
| 581 | |
| 582 | found_multi = false; |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 583 | end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1, |
| 584 | PCI_MAX_PCI_FUNCTIONS - 1); |
| 585 | for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end; |
| 586 | bdf += PCI_BDF(0, 0, 1)) { |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 587 | struct pci_child_platdata *pplat; |
| 588 | struct udevice *dev; |
| 589 | ulong class; |
| 590 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 591 | if (PCI_FUNC(bdf) && !found_multi) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 592 | continue; |
| 593 | /* Check only the first access, we don't expect problems */ |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 594 | ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 595 | &header_type, PCI_SIZE_8); |
| 596 | if (ret) |
| 597 | goto error; |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 598 | pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 599 | PCI_SIZE_16); |
| 600 | if (vendor == 0xffff || vendor == 0x0000) |
| 601 | continue; |
| 602 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 603 | if (!PCI_FUNC(bdf)) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 604 | found_multi = header_type & 0x80; |
| 605 | |
| 606 | debug("%s: bus %d/%s: found device %x, function %d\n", __func__, |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 607 | bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 608 | pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 609 | PCI_SIZE_16); |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 610 | pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class, |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 611 | PCI_SIZE_32); |
| 612 | class >>= 8; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 613 | |
| 614 | /* Find this device in the device tree */ |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 615 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 616 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 617 | /* Search for a driver */ |
| 618 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 619 | /* If nothing in the device tree, bind a generic device */ |
| 620 | if (ret == -ENODEV) { |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 621 | struct pci_device_id find_id; |
| 622 | ulong val; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 623 | |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 624 | memset(&find_id, '\0', sizeof(find_id)); |
| 625 | find_id.vendor = vendor; |
| 626 | find_id.device = device; |
| 627 | find_id.class = class; |
| 628 | if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) { |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 629 | pci_bus_read_config(bus, bdf, |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 630 | PCI_SUBSYSTEM_VENDOR_ID, |
| 631 | &val, PCI_SIZE_32); |
| 632 | find_id.subvendor = val & 0xffff; |
| 633 | find_id.subdevice = val >> 16; |
| 634 | } |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 635 | ret = pci_find_and_bind_driver(bus, &find_id, bdf, |
Simon Glass | aba9296 | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 636 | &dev); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 637 | } |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 638 | if (ret == -EPERM) |
| 639 | continue; |
| 640 | else if (ret) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 641 | return ret; |
| 642 | |
| 643 | /* Update the platform data */ |
Simon Glass | 5dbcf3a | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 644 | pplat = dev_get_parent_platdata(dev); |
| 645 | pplat->devfn = PCI_MASK_BUS(bdf); |
| 646 | pplat->vendor = vendor; |
| 647 | pplat->device = device; |
| 648 | pplat->class = class; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | return 0; |
| 652 | error: |
| 653 | printf("Cannot read bus configuration: %d\n", ret); |
| 654 | |
| 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | static int pci_uclass_post_bind(struct udevice *bus) |
| 659 | { |
| 660 | /* |
Bin Meng | 1887ed3 | 2015-08-24 01:14:01 -0700 | [diff] [blame] | 661 | * If there is no pci device listed in the device tree, |
| 662 | * don't bother scanning the device tree. |
| 663 | */ |
| 664 | if (bus->of_offset == -1) |
| 665 | return 0; |
| 666 | |
| 667 | /* |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 668 | * Scan the device tree for devices. This does not probe the PCI bus, |
| 669 | * as this is not permitted while binding. It just finds devices |
| 670 | * mentioned in the device tree. |
| 671 | * |
| 672 | * Before relocation, only bind devices marked for pre-relocation |
| 673 | * use. |
| 674 | */ |
| 675 | return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset, |
| 676 | gd->flags & GD_FLG_RELOC ? false : true); |
| 677 | } |
| 678 | |
| 679 | static int decode_regions(struct pci_controller *hose, const void *blob, |
| 680 | int parent_node, int node) |
| 681 | { |
| 682 | int pci_addr_cells, addr_cells, size_cells; |
Simon Glass | 2084c5a | 2015-11-19 20:26:57 -0700 | [diff] [blame] | 683 | phys_addr_t base = 0, size; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 684 | int cells_per_record; |
| 685 | const u32 *prop; |
| 686 | int len; |
| 687 | int i; |
| 688 | |
| 689 | prop = fdt_getprop(blob, node, "ranges", &len); |
| 690 | if (!prop) |
| 691 | return -EINVAL; |
| 692 | pci_addr_cells = fdt_address_cells(blob, node); |
| 693 | addr_cells = fdt_address_cells(blob, parent_node); |
| 694 | size_cells = fdt_size_cells(blob, node); |
| 695 | |
| 696 | /* PCI addresses are always 3-cells */ |
| 697 | len /= sizeof(u32); |
| 698 | cells_per_record = pci_addr_cells + addr_cells + size_cells; |
| 699 | hose->region_count = 0; |
| 700 | debug("%s: len=%d, cells_per_record=%d\n", __func__, len, |
| 701 | cells_per_record); |
| 702 | for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) { |
| 703 | u64 pci_addr, addr, size; |
| 704 | int space_code; |
| 705 | u32 flags; |
| 706 | int type; |
Simon Glass | 9526d83 | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 707 | int pos; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 708 | |
| 709 | if (len < cells_per_record) |
| 710 | break; |
| 711 | flags = fdt32_to_cpu(prop[0]); |
| 712 | space_code = (flags >> 24) & 3; |
| 713 | pci_addr = fdtdec_get_number(prop + 1, 2); |
| 714 | prop += pci_addr_cells; |
| 715 | addr = fdtdec_get_number(prop, addr_cells); |
| 716 | prop += addr_cells; |
| 717 | size = fdtdec_get_number(prop, size_cells); |
| 718 | prop += size_cells; |
| 719 | debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64 |
| 720 | ", size=%" PRIx64 ", space_code=%d\n", __func__, |
| 721 | hose->region_count, pci_addr, addr, size, space_code); |
| 722 | if (space_code & 2) { |
| 723 | type = flags & (1U << 30) ? PCI_REGION_PREFETCH : |
| 724 | PCI_REGION_MEM; |
| 725 | } else if (space_code & 1) { |
| 726 | type = PCI_REGION_IO; |
| 727 | } else { |
| 728 | continue; |
| 729 | } |
Simon Glass | 9526d83 | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 730 | pos = -1; |
| 731 | for (i = 0; i < hose->region_count; i++) { |
| 732 | if (hose->regions[i].flags == type) |
| 733 | pos = i; |
| 734 | } |
| 735 | if (pos == -1) |
| 736 | pos = hose->region_count++; |
| 737 | debug(" - type=%d, pos=%d\n", type, pos); |
| 738 | pci_set_region(hose->regions + pos, pci_addr, addr, size, type); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* Add a region for our local memory */ |
Simon Glass | 2084c5a | 2015-11-19 20:26:57 -0700 | [diff] [blame] | 742 | size = gd->ram_size; |
| 743 | #ifdef CONFIG_SYS_SDRAM_BASE |
| 744 | base = CONFIG_SYS_SDRAM_BASE; |
| 745 | #endif |
| 746 | if (gd->pci_ram_top && gd->pci_ram_top < base + size) |
| 747 | size = gd->pci_ram_top - base; |
| 748 | pci_set_region(hose->regions + hose->region_count++, base, base, |
| 749 | size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static int pci_uclass_pre_probe(struct udevice *bus) |
| 755 | { |
| 756 | struct pci_controller *hose; |
| 757 | int ret; |
| 758 | |
| 759 | debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name, |
| 760 | bus->parent->name); |
| 761 | hose = bus->uclass_priv; |
| 762 | |
| 763 | /* For bridges, use the top-level PCI controller */ |
| 764 | if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) { |
| 765 | hose->ctlr = bus; |
| 766 | ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset, |
| 767 | bus->of_offset); |
| 768 | if (ret) { |
| 769 | debug("%s: Cannot decode regions\n", __func__); |
| 770 | return ret; |
| 771 | } |
| 772 | } else { |
| 773 | struct pci_controller *parent_hose; |
| 774 | |
| 775 | parent_hose = dev_get_uclass_priv(bus->parent); |
| 776 | hose->ctlr = parent_hose->bus; |
| 777 | } |
| 778 | hose->bus = bus; |
| 779 | hose->first_busno = bus->seq; |
| 780 | hose->last_busno = bus->seq; |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static int pci_uclass_post_probe(struct udevice *bus) |
| 786 | { |
| 787 | int ret; |
| 788 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 789 | debug("%s: probing bus %d\n", __func__, bus->seq); |
| 790 | ret = pci_bind_bus_devices(bus); |
| 791 | if (ret) |
| 792 | return ret; |
| 793 | |
| 794 | #ifdef CONFIG_PCI_PNP |
| 795 | ret = pci_auto_config_devices(bus); |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 796 | if (ret < 0) |
| 797 | return ret; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 798 | #endif |
| 799 | |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 800 | #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) |
| 801 | /* |
| 802 | * Per Intel FSP specification, we should call FSP notify API to |
| 803 | * inform FSP that PCI enumeration has been done so that FSP will |
| 804 | * do any necessary initialization as required by the chipset's |
| 805 | * BIOS Writer's Guide (BWG). |
| 806 | * |
| 807 | * Unfortunately we have to put this call here as with driver model, |
| 808 | * the enumeration is all done on a lazy basis as needed, so until |
| 809 | * something is touched on PCI it won't happen. |
| 810 | * |
| 811 | * Note we only call this 1) after U-Boot is relocated, and 2) |
| 812 | * root bus has finished probing. |
| 813 | */ |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 814 | if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) { |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 815 | ret = fsp_init_phase_pci(); |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 816 | if (ret) |
| 817 | return ret; |
| 818 | } |
Bin Meng | 348b744 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 819 | #endif |
| 820 | |
Simon Glass | 4d21455 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 821 | return 0; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static int pci_uclass_child_post_bind(struct udevice *dev) |
| 825 | { |
| 826 | struct pci_child_platdata *pplat; |
| 827 | struct fdt_pci_addr addr; |
| 828 | int ret; |
| 829 | |
| 830 | if (dev->of_offset == -1) |
| 831 | return 0; |
| 832 | |
| 833 | /* |
| 834 | * We could read vendor, device, class if available. But for now we |
| 835 | * just check the address. |
| 836 | */ |
| 837 | pplat = dev_get_parent_platdata(dev); |
| 838 | ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset, |
| 839 | FDT_PCI_SPACE_CONFIG, "reg", &addr); |
| 840 | |
| 841 | if (ret) { |
| 842 | if (ret != -ENOENT) |
| 843 | return -EINVAL; |
| 844 | } else { |
Bin Meng | dce54dd | 2015-08-20 06:40:26 -0700 | [diff] [blame] | 845 | /* extract the devfn from fdt_pci_addr */ |
| 846 | pplat->devfn = addr.phys_hi & 0xff00; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 852 | static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf, |
| 853 | uint offset, ulong *valuep, |
| 854 | enum pci_size_t size) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 855 | { |
| 856 | struct pci_controller *hose = bus->uclass_priv; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 857 | |
| 858 | return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size); |
| 859 | } |
| 860 | |
Bin Meng | 4d8615c | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 861 | static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf, |
| 862 | uint offset, ulong value, |
| 863 | enum pci_size_t size) |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 864 | { |
| 865 | struct pci_controller *hose = bus->uclass_priv; |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 866 | |
| 867 | return pci_bus_write_config(hose->ctlr, bdf, offset, value, size); |
| 868 | } |
| 869 | |
Simon Glass | 76c3fbc | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 870 | static int skip_to_next_device(struct udevice *bus, struct udevice **devp) |
| 871 | { |
| 872 | struct udevice *dev; |
| 873 | int ret = 0; |
| 874 | |
| 875 | /* |
| 876 | * Scan through all the PCI controllers. On x86 there will only be one |
| 877 | * but that is not necessarily true on other hardware. |
| 878 | */ |
| 879 | do { |
| 880 | device_find_first_child(bus, &dev); |
| 881 | if (dev) { |
| 882 | *devp = dev; |
| 883 | return 0; |
| 884 | } |
| 885 | ret = uclass_next_device(&bus); |
| 886 | if (ret) |
| 887 | return ret; |
| 888 | } while (bus); |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | int pci_find_next_device(struct udevice **devp) |
| 894 | { |
| 895 | struct udevice *child = *devp; |
| 896 | struct udevice *bus = child->parent; |
| 897 | int ret; |
| 898 | |
| 899 | /* First try all the siblings */ |
| 900 | *devp = NULL; |
| 901 | while (child) { |
| 902 | device_find_next_child(&child); |
| 903 | if (child) { |
| 904 | *devp = child; |
| 905 | return 0; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /* We ran out of siblings. Try the next bus */ |
| 910 | ret = uclass_next_device(&bus); |
| 911 | if (ret) |
| 912 | return ret; |
| 913 | |
| 914 | return bus ? skip_to_next_device(bus, devp) : 0; |
| 915 | } |
| 916 | |
| 917 | int pci_find_first_device(struct udevice **devp) |
| 918 | { |
| 919 | struct udevice *bus; |
| 920 | int ret; |
| 921 | |
| 922 | *devp = NULL; |
| 923 | ret = uclass_first_device(UCLASS_PCI, &bus); |
| 924 | if (ret) |
| 925 | return ret; |
| 926 | |
| 927 | return skip_to_next_device(bus, devp); |
| 928 | } |
| 929 | |
Simon Glass | 9289db6 | 2015-11-19 20:26:59 -0700 | [diff] [blame] | 930 | ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size) |
| 931 | { |
| 932 | switch (size) { |
| 933 | case PCI_SIZE_8: |
| 934 | return (value >> ((offset & 3) * 8)) & 0xff; |
| 935 | case PCI_SIZE_16: |
| 936 | return (value >> ((offset & 2) * 8)) & 0xffff; |
| 937 | default: |
| 938 | return value; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | ulong pci_conv_size_to_32(ulong old, ulong value, uint offset, |
| 943 | enum pci_size_t size) |
| 944 | { |
| 945 | uint off_mask; |
| 946 | uint val_mask, shift; |
| 947 | ulong ldata, mask; |
| 948 | |
| 949 | switch (size) { |
| 950 | case PCI_SIZE_8: |
| 951 | off_mask = 3; |
| 952 | val_mask = 0xff; |
| 953 | break; |
| 954 | case PCI_SIZE_16: |
| 955 | off_mask = 2; |
| 956 | val_mask = 0xffff; |
| 957 | break; |
| 958 | default: |
| 959 | return value; |
| 960 | } |
| 961 | shift = (offset & off_mask) * 8; |
| 962 | ldata = (value & val_mask) << shift; |
| 963 | mask = val_mask << shift; |
| 964 | value = (old & ~mask) | ldata; |
| 965 | |
| 966 | return value; |
| 967 | } |
| 968 | |
Simon Glass | f926033 | 2015-11-19 20:27:01 -0700 | [diff] [blame] | 969 | int pci_get_regions(struct udevice *dev, struct pci_region **iop, |
| 970 | struct pci_region **memp, struct pci_region **prefp) |
| 971 | { |
| 972 | struct udevice *bus = pci_get_controller(dev); |
| 973 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
| 974 | int i; |
| 975 | |
| 976 | *iop = NULL; |
| 977 | *memp = NULL; |
| 978 | *prefp = NULL; |
| 979 | for (i = 0; i < hose->region_count; i++) { |
| 980 | switch (hose->regions[i].flags) { |
| 981 | case PCI_REGION_IO: |
| 982 | if (!*iop || (*iop)->size < hose->regions[i].size) |
| 983 | *iop = hose->regions + i; |
| 984 | break; |
| 985 | case PCI_REGION_MEM: |
| 986 | if (!*memp || (*memp)->size < hose->regions[i].size) |
| 987 | *memp = hose->regions + i; |
| 988 | break; |
| 989 | case (PCI_REGION_MEM | PCI_REGION_PREFETCH): |
| 990 | if (!*prefp || (*prefp)->size < hose->regions[i].size) |
| 991 | *prefp = hose->regions + i; |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL); |
| 997 | } |
| 998 | |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 999 | UCLASS_DRIVER(pci) = { |
| 1000 | .id = UCLASS_PCI, |
| 1001 | .name = "pci", |
Simon Glass | 2bb02e4 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 1002 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1003 | .post_bind = pci_uclass_post_bind, |
| 1004 | .pre_probe = pci_uclass_pre_probe, |
| 1005 | .post_probe = pci_uclass_post_probe, |
| 1006 | .child_post_bind = pci_uclass_child_post_bind, |
| 1007 | .per_device_auto_alloc_size = sizeof(struct pci_controller), |
| 1008 | .per_child_platdata_auto_alloc_size = |
| 1009 | sizeof(struct pci_child_platdata), |
| 1010 | }; |
| 1011 | |
| 1012 | static const struct dm_pci_ops pci_bridge_ops = { |
| 1013 | .read_config = pci_bridge_read_config, |
| 1014 | .write_config = pci_bridge_write_config, |
| 1015 | }; |
| 1016 | |
| 1017 | static const struct udevice_id pci_bridge_ids[] = { |
| 1018 | { .compatible = "pci-bridge" }, |
| 1019 | { } |
| 1020 | }; |
| 1021 | |
| 1022 | U_BOOT_DRIVER(pci_bridge_drv) = { |
| 1023 | .name = "pci_bridge_drv", |
| 1024 | .id = UCLASS_PCI, |
| 1025 | .of_match = pci_bridge_ids, |
| 1026 | .ops = &pci_bridge_ops, |
| 1027 | }; |
| 1028 | |
| 1029 | UCLASS_DRIVER(pci_generic) = { |
| 1030 | .id = UCLASS_PCI_GENERIC, |
| 1031 | .name = "pci_generic", |
| 1032 | }; |
| 1033 | |
| 1034 | static const struct udevice_id pci_generic_ids[] = { |
| 1035 | { .compatible = "pci-generic" }, |
| 1036 | { } |
| 1037 | }; |
| 1038 | |
| 1039 | U_BOOT_DRIVER(pci_generic_drv) = { |
| 1040 | .name = "pci_generic_drv", |
| 1041 | .id = UCLASS_PCI_GENERIC, |
| 1042 | .of_match = pci_generic_ids, |
| 1043 | }; |