Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
| 4 | * It was originally based on the Linux kernel (drivers/pci/pci.c). |
| 5 | * |
| 6 | * Modifications are: |
| 7 | * Copyright (C) 2003-2004 Linux Networx |
| 8 | * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx) |
| 9 | * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com> |
| 10 | * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov> |
| 11 | * Copyright (C) 2005-2006 Tyan |
| 12 | * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan) |
Patrick Georgi | 16cdbb2 | 2009-04-21 20:14:31 +0000 | [diff] [blame] | 13 | * Copyright (C) 2005-2009 coresystems GmbH |
| 14 | * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH) |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 18 | * PCI Bus Services, see include/linux/pci.h for further explanation. |
| 19 | * |
| 20 | * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter, |
| 21 | * David Mosberger-Tang |
| 22 | * |
| 23 | * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <console/console.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stdint.h> |
| 29 | #include <bitops.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | #include <string.h> |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 31 | #include <arch/io.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 32 | #include <device/device.h> |
| 33 | #include <device/pci.h> |
| 34 | #include <device/pci_ids.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 35 | #include <part/hard_reset.h> |
Eric Biederman | 30e143a | 2003-09-01 23:45:32 +0000 | [diff] [blame] | 36 | #include <part/fallback_boot.h> |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 37 | #include <delay.h> |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 38 | #if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1 |
| 39 | #include <device/hypertransport.h> |
| 40 | #endif |
| 41 | #if CONFIG_PCIX_PLUGIN_SUPPORT == 1 |
| 42 | #include <device/pcix.h> |
| 43 | #endif |
| 44 | #if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1 |
| 45 | #include <device/pciexp.h> |
| 46 | #endif |
Stefan Reinauer | ec75a57 | 2009-03-16 15:27:00 +0000 | [diff] [blame] | 47 | #if CONFIG_AGP_PLUGIN_SUPPORT == 1 |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 48 | #include <device/agp.h> |
| 49 | #endif |
| 50 | #if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1 |
| 51 | #include <device/cardbus.h> |
| 52 | #endif |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 53 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 54 | uint8_t pci_moving_config8(struct device *dev, unsigned reg) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 55 | { |
| 56 | uint8_t value, ones, zeroes; |
| 57 | value = pci_read_config8(dev, reg); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 58 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 59 | pci_write_config8(dev, reg, 0xff); |
| 60 | ones = pci_read_config8(dev, reg); |
| 61 | |
| 62 | pci_write_config8(dev, reg, 0x00); |
| 63 | zeroes = pci_read_config8(dev, reg); |
| 64 | |
| 65 | pci_write_config8(dev, reg, value); |
| 66 | |
| 67 | return ones ^ zeroes; |
| 68 | } |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 69 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 70 | uint16_t pci_moving_config16(struct device *dev, unsigned reg) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 71 | { |
| 72 | uint16_t value, ones, zeroes; |
| 73 | value = pci_read_config16(dev, reg); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 74 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 75 | pci_write_config16(dev, reg, 0xffff); |
| 76 | ones = pci_read_config16(dev, reg); |
| 77 | |
| 78 | pci_write_config16(dev, reg, 0x0000); |
| 79 | zeroes = pci_read_config16(dev, reg); |
| 80 | |
| 81 | pci_write_config16(dev, reg, value); |
| 82 | |
| 83 | return ones ^ zeroes; |
| 84 | } |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 85 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 86 | uint32_t pci_moving_config32(struct device *dev, unsigned reg) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 87 | { |
| 88 | uint32_t value, ones, zeroes; |
| 89 | value = pci_read_config32(dev, reg); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 90 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 91 | pci_write_config32(dev, reg, 0xffffffff); |
| 92 | ones = pci_read_config32(dev, reg); |
| 93 | |
| 94 | pci_write_config32(dev, reg, 0x00000000); |
| 95 | zeroes = pci_read_config32(dev, reg); |
| 96 | |
| 97 | pci_write_config32(dev, reg, value); |
| 98 | |
| 99 | return ones ^ zeroes; |
| 100 | } |
| 101 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 102 | unsigned pci_find_next_capability(device_t dev, unsigned cap, unsigned last) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 103 | { |
| 104 | unsigned pos; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 105 | unsigned status; |
| 106 | unsigned reps = 48; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 107 | pos = 0; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 108 | status = pci_read_config16(dev, PCI_STATUS); |
| 109 | if (!(status & PCI_STATUS_CAP_LIST)) { |
| 110 | return 0; |
| 111 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 112 | switch(dev->hdr_type & 0x7f) { |
| 113 | case PCI_HEADER_TYPE_NORMAL: |
| 114 | case PCI_HEADER_TYPE_BRIDGE: |
| 115 | pos = PCI_CAPABILITY_LIST; |
| 116 | break; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 117 | case PCI_HEADER_TYPE_CARDBUS: |
| 118 | pos = PCI_CB_CAPABILITY_LIST; |
| 119 | break; |
| 120 | default: |
| 121 | return 0; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 122 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 123 | pos = pci_read_config8(dev, pos); |
| 124 | while(reps-- && (pos >= 0x40)) { /* loop through the linked list */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 125 | int this_cap; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 126 | pos &= ~3; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 127 | this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 128 | printk_spew("Capability: 0x%02x @ 0x%02x\n", cap, pos); |
| 129 | if (this_cap == 0xff) { |
| 130 | break; |
| 131 | } |
| 132 | if (!last && (this_cap == cap)) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 133 | return pos; |
| 134 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 135 | if (last == pos) { |
| 136 | last = 0; |
| 137 | } |
| 138 | pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 143 | unsigned pci_find_capability(device_t dev, unsigned cap) |
| 144 | { |
| 145 | return pci_find_next_capability(dev, cap, 0); |
| 146 | |
| 147 | } |
| 148 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 149 | /** Given a device and register, read the size of the BAR for that register. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 150 | * @param dev Pointer to the device structure |
| 151 | * @param resource Pointer to the resource structure |
| 152 | * @param index Address of the pci configuration register |
| 153 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 154 | struct resource *pci_get_resource(struct device *dev, unsigned long index) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 155 | { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 156 | struct resource *resource; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 157 | unsigned long value, attr; |
| 158 | resource_t moving, limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 159 | |
| 160 | /* Initialize the resources to nothing */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 161 | resource = new_resource(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 162 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 163 | /* Get the initial value */ |
| 164 | value = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 165 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 166 | /* See which bits move */ |
| 167 | moving = pci_moving_config32(dev, index); |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 168 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 169 | /* Initialize attr to the bits that do not move */ |
| 170 | attr = value & ~moving; |
| 171 | |
| 172 | /* If it is a 64bit resource look at the high half as well */ |
| 173 | if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) && |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 174 | ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) == PCI_BASE_ADDRESS_MEM_LIMIT_64)) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 175 | { |
| 176 | /* Find the high bits that move */ |
| 177 | moving |= ((resource_t)pci_moving_config32(dev, index + 4)) << 32; |
| 178 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 179 | /* Find the resource constraints. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 180 | * |
| 181 | * Start by finding the bits that move. From there: |
| 182 | * - Size is the least significant bit of the bits that move. |
| 183 | * - Limit is all of the bits that move plus all of the lower bits. |
| 184 | * See PCI Spec 6.2.5.1 ... |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 185 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 186 | limit = 0; |
| 187 | if (moving) { |
| 188 | resource->size = 1; |
| 189 | resource->align = resource->gran = 0; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 190 | while(!(moving & resource->size)) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 191 | resource->size <<= 1; |
| 192 | resource->align += 1; |
| 193 | resource->gran += 1; |
| 194 | } |
| 195 | resource->limit = limit = moving | (resource->size - 1); |
| 196 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 197 | /* |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 198 | * some broken hardware has read-only registers that do not |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 199 | * really size correctly. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 200 | * Example: the acer m7229 has BARs 1-4 normally read-only. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 201 | * so BAR1 at offset 0x10 reads 0x1f1. If you size that register |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 202 | * by writing 0xffffffff to it, it will read back as 0x1f1 -- a |
| 203 | * violation of the spec. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 204 | * We catch this case and ignore it by observing which bits move, |
| 205 | * This also catches the common case unimplemented registers |
| 206 | * that always read back as 0. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 207 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 208 | if (moving == 0) { |
| 209 | if (value != 0) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 210 | printk_debug( |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 211 | "%s register %02lx(%08lx), read-only ignoring it\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 212 | dev_path(dev), index, value); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 213 | } |
| 214 | resource->flags = 0; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 215 | } |
| 216 | else if (attr & PCI_BASE_ADDRESS_SPACE_IO) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 217 | /* An I/O mapped base address */ |
| 218 | attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 219 | resource->flags |= IORESOURCE_IO; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 220 | /* I don't want to deal with 32bit I/O resources */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 221 | resource->limit = 0xffff; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 222 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 223 | else { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 224 | /* A Memory mapped base address */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 225 | attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 226 | resource->flags |= IORESOURCE_MEM; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 227 | if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 228 | resource->flags |= IORESOURCE_PREFETCH; |
| 229 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 230 | attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK; |
| 231 | if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 232 | /* 32bit limit */ |
| 233 | resource->limit = 0xffffffffUL; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 234 | } |
| 235 | else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 236 | /* 1MB limit */ |
| 237 | resource->limit = 0x000fffffUL; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 238 | } |
| 239 | else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 240 | /* 64bit limit */ |
| 241 | resource->limit = 0xffffffffffffffffULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 242 | resource->flags |= IORESOURCE_PCI64; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 243 | } |
| 244 | else { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 245 | /* Invalid value */ |
| 246 | resource->flags = 0; |
| 247 | } |
| 248 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 249 | /* Don't let the limit exceed which bits can move */ |
| 250 | if (resource->limit > limit) { |
| 251 | resource->limit = limit; |
| 252 | } |
| 253 | #if 0 |
| 254 | if (resource->flags) { |
| 255 | printk_debug("%s %02x ->", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 256 | dev_path(dev), resource->index); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 257 | printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 258 | value, zeroes, ones, attr); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 259 | printk_debug( |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 260 | "%s %02x -> size: 0x%08Lx max: 0x%08Lx %s\n ", |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 261 | dev_path(dev), |
| 262 | resource->index, |
| 263 | resource->size, resource->limit, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 264 | resource_type(resource)); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 265 | } |
| 266 | #endif |
| 267 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 268 | return resource; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 271 | static void pci_get_rom_resource(struct device *dev, unsigned long index) |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 272 | { |
| 273 | struct resource *resource; |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 274 | unsigned long value; |
Patrick Georgi | 16cdbb2 | 2009-04-21 20:14:31 +0000 | [diff] [blame] | 275 | resource_t moving; |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 276 | |
Li-Ta Lo | bec039c | 2005-01-19 23:19:26 +0000 | [diff] [blame] | 277 | if ((dev->on_mainboard) && (dev->rom_address == 0)) { |
| 278 | //skip it if rom_address is not set in MB Config.lb |
Yinghai Lu | bcde161 | 2005-01-14 05:34:09 +0000 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 282 | /* Initialize the resources to nothing */ |
| 283 | resource = new_resource(dev, index); |
| 284 | |
| 285 | /* Get the initial value */ |
| 286 | value = pci_read_config32(dev, index); |
| 287 | |
| 288 | /* See which bits move */ |
| 289 | moving = pci_moving_config32(dev, index); |
| 290 | /* clear the Enable bit */ |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 291 | moving = moving & ~PCI_ROM_ADDRESS_ENABLE; |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 292 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 293 | /* Find the resource constraints. |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 294 | * |
| 295 | * Start by finding the bits that move. From there: |
| 296 | * - Size is the least significant bit of the bits that move. |
| 297 | * - Limit is all of the bits that move plus all of the lower bits. |
| 298 | * See PCI Spec 6.2.5.1 ... |
| 299 | */ |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 300 | if (moving) { |
| 301 | resource->size = 1; |
| 302 | resource->align = resource->gran = 0; |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 303 | while (!(moving & resource->size)) { |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 304 | resource->size <<= 1; |
| 305 | resource->align += 1; |
| 306 | resource->gran += 1; |
| 307 | } |
Patrick Georgi | 16cdbb2 | 2009-04-21 20:14:31 +0000 | [diff] [blame] | 308 | resource->limit = moving | (resource->size - 1); |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if (moving == 0) { |
| 312 | if (value != 0) { |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 313 | printk_debug("%s register %02lx(%08lx), read-only ignoring it\n", |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 314 | dev_path(dev), index, value); |
| 315 | } |
| 316 | resource->flags = 0; |
| 317 | } else { |
| 318 | resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY; |
| 319 | } |
Yinghai Lu | c7870ac | 2005-01-13 19:14:52 +0000 | [diff] [blame] | 320 | |
| 321 | /* for on board device with embedded ROM image, the ROM image is at |
| 322 | * fixed address specified in the Config.lb, the dev->rom_address is |
| 323 | * inited by driver_pci_onboard_ops::enable_dev() */ |
Yinghai Lu | bcde161 | 2005-01-14 05:34:09 +0000 | [diff] [blame] | 324 | if ((dev->on_mainboard) && (dev->rom_address != 0)) { |
Yinghai Lu | c7870ac | 2005-01-13 19:14:52 +0000 | [diff] [blame] | 325 | resource->base = dev->rom_address; |
| 326 | resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY | |
| 327 | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 328 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 329 | |
| 330 | compact_resources(dev); |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 331 | } |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 332 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 333 | /** Read the base address registers for a given device. |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 334 | * @param dev Pointer to the dev structure |
| 335 | * @param howmany How many registers to read (6 for device, 2 for bridge) |
| 336 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 337 | static void pci_read_bases(struct device *dev, unsigned int howmany) |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 338 | { |
| 339 | unsigned long index; |
| 340 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 341 | for(index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) { |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 342 | struct resource *resource; |
| 343 | resource = pci_get_resource(dev, index); |
| 344 | index += (resource->flags & IORESOURCE_PCI64)?8:4; |
| 345 | } |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 346 | |
| 347 | compact_resources(dev); |
Li-Ta Lo | 9a5b496 | 2004-12-23 21:48:01 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 350 | static void pci_set_resource(struct device *dev, struct resource *resource); |
| 351 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 352 | static void pci_record_bridge_resource( |
| 353 | struct device *dev, resource_t moving, |
| 354 | unsigned index, unsigned long mask, unsigned long type) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 355 | { |
| 356 | /* Initiliaze the constraints on the current bus */ |
| 357 | struct resource *resource; |
| 358 | resource = 0; |
| 359 | if (moving) { |
| 360 | unsigned long gran; |
| 361 | resource_t step; |
| 362 | resource = new_resource(dev, index); |
| 363 | resource->size = 0; |
| 364 | gran = 0; |
| 365 | step = 1; |
| 366 | while((moving & step) == 0) { |
| 367 | gran += 1; |
| 368 | step <<= 1; |
| 369 | } |
| 370 | resource->gran = gran; |
| 371 | resource->align = gran; |
| 372 | resource->limit = moving | (step - 1); |
| 373 | resource->flags = type | IORESOURCE_PCI_BRIDGE; |
| 374 | compute_allocate_resource(&dev->link[0], resource, mask, type); |
| 375 | /* If there is nothing behind the resource, |
| 376 | * clear it and forget it. |
| 377 | */ |
| 378 | if (resource->size == 0) { |
Stefan Reinauer | 532fd2d | 2008-10-29 03:15:42 +0000 | [diff] [blame] | 379 | #if CONFIG_PCI_64BIT_PREF_MEM == 1 |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 380 | resource->base = moving; |
Stefan Reinauer | 532fd2d | 2008-10-29 03:15:42 +0000 | [diff] [blame] | 381 | #else |
| 382 | resource->base = moving & 0xffffffff; |
| 383 | #endif |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 384 | resource->flags |= IORESOURCE_ASSIGNED; |
| 385 | resource->flags &= ~IORESOURCE_STORED; |
| 386 | pci_set_resource(dev, resource); |
| 387 | resource->flags = 0; |
| 388 | } |
| 389 | } |
| 390 | return; |
| 391 | } |
| 392 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 393 | static void pci_bridge_read_bases(struct device *dev) |
| 394 | { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 395 | resource_t moving_base, moving_limit, moving; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 396 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 397 | /* See if the bridge I/O resources are implemented */ |
| 398 | moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8; |
| 399 | moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16; |
| 400 | |
| 401 | moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8; |
| 402 | moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16; |
| 403 | |
| 404 | moving = moving_base & moving_limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 405 | |
| 406 | /* Initialize the io space constraints on the current bus */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 407 | pci_record_bridge_resource( |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 408 | dev, moving, PCI_IO_BASE, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 409 | IORESOURCE_IO, IORESOURCE_IO); |
| 410 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 411 | |
| 412 | /* See if the bridge prefmem resources are implemented */ |
| 413 | moving_base = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16; |
| 414 | moving_base |= ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32; |
| 415 | |
| 416 | moving_limit = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16; |
| 417 | moving_limit |= ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 418 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 419 | moving = moving_base & moving_limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 420 | /* Initiliaze the prefetchable memory constraints on the current bus */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 421 | pci_record_bridge_resource( |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 422 | dev, moving, PCI_PREF_MEMORY_BASE, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 423 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 424 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 425 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 426 | |
| 427 | /* See if the bridge mem resources are implemented */ |
| 428 | moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16; |
| 429 | moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16; |
| 430 | |
| 431 | moving = moving_base & moving_limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 432 | |
| 433 | /* Initialize the memory resources on the current bus */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 434 | pci_record_bridge_resource( |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 435 | dev, moving, PCI_MEMORY_BASE, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 436 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 437 | IORESOURCE_MEM); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 438 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 439 | compact_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 442 | void pci_dev_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 443 | { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 444 | pci_read_bases(dev, 6); |
| 445 | pci_get_rom_resource(dev, PCI_ROM_ADDRESS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 448 | void pci_bus_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 449 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 450 | pci_bridge_read_bases(dev); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 451 | pci_read_bases(dev, 2); |
| 452 | pci_get_rom_resource(dev, PCI_ROM_ADDRESS1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 455 | static void pci_set_resource(struct device *dev, struct resource *resource) |
| 456 | { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 457 | resource_t base, end; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 458 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 459 | /* Make certain the resource has actually been set */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 460 | if (!(resource->flags & IORESOURCE_ASSIGNED)) { |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 461 | printk_err("ERROR: %s %02lx %s size: 0x%010Lx not assigned\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 462 | dev_path(dev), resource->index, |
| 463 | resource_type(resource), |
| 464 | resource->size); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 468 | /* If I have already stored this resource don't worry about it */ |
| 469 | if (resource->flags & IORESOURCE_STORED) { |
| 470 | return; |
| 471 | } |
| 472 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 473 | /* If the resources is substractive don't worry about it */ |
| 474 | if (resource->flags & IORESOURCE_SUBTRACTIVE) { |
| 475 | return; |
| 476 | } |
| 477 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 478 | /* Only handle PCI memory and IO resources for now */ |
| 479 | if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO))) |
| 480 | return; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 481 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 482 | /* Enable the resources in the command register */ |
| 483 | if (resource->size) { |
| 484 | if (resource->flags & IORESOURCE_MEM) { |
| 485 | dev->command |= PCI_COMMAND_MEMORY; |
| 486 | } |
| 487 | if (resource->flags & IORESOURCE_IO) { |
| 488 | dev->command |= PCI_COMMAND_IO; |
| 489 | } |
| 490 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
| 491 | dev->command |= PCI_COMMAND_MASTER; |
| 492 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 493 | } |
| 494 | /* Get the base address */ |
| 495 | base = resource->base; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 496 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 497 | /* Get the end */ |
| 498 | end = resource_end(resource); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 499 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 500 | /* Now store the resource */ |
| 501 | resource->flags |= IORESOURCE_STORED; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 502 | if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 503 | unsigned long base_lo, base_hi; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 504 | /* |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 505 | * some chipsets allow us to set/clear the IO bit. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 506 | * (e.g. VIA 82c686a.) So set it to be safe) |
| 507 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 508 | base_lo = base & 0xffffffff; |
| 509 | base_hi = (base >> 32) & 0xffffffff; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 510 | if (resource->flags & IORESOURCE_IO) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 511 | base_lo |= PCI_BASE_ADDRESS_SPACE_IO; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 512 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 513 | pci_write_config32(dev, resource->index, base_lo); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 514 | if (resource->flags & IORESOURCE_PCI64) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 515 | pci_write_config32(dev, resource->index + 4, base_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 516 | } |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 517 | } |
| 518 | else if (resource->index == PCI_IO_BASE) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 519 | /* set the IO ranges */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 520 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 521 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 522 | pci_write_config8(dev, PCI_IO_BASE, base >> 8); |
| 523 | pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16); |
| 524 | pci_write_config8(dev, PCI_IO_LIMIT, end >> 8); |
| 525 | pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 526 | } |
| 527 | else if (resource->index == PCI_MEMORY_BASE) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 528 | /* set the memory range */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 529 | compute_allocate_resource(&dev->link[0], resource, |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 530 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 531 | IORESOURCE_MEM); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 532 | pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 533 | pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 534 | } |
| 535 | else if (resource->index == PCI_PREF_MEMORY_BASE) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 536 | /* set the prefetchable memory range */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 537 | compute_allocate_resource(&dev->link[0], resource, |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 538 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 539 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 540 | pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16); |
| 541 | pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32); |
| 542 | pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16); |
| 543 | pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 544 | } |
| 545 | else { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 546 | /* Don't let me think I stored the resource */ |
| 547 | resource->flags &= ~IORESOURCE_STORED; |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 548 | printk_err("ERROR: invalid resource->index %lx\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 549 | resource->index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 550 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 551 | report_resource_stored(dev, resource, ""); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 555 | void pci_dev_set_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 556 | { |
| 557 | struct resource *resource, *last; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 558 | unsigned link; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 559 | uint8_t line; |
| 560 | |
| 561 | last = &dev->resource[dev->resources]; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 562 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 563 | for(resource = &dev->resource[0]; resource < last; resource++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 564 | pci_set_resource(dev, resource); |
| 565 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 566 | for(link = 0; link < dev->links; link++) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 567 | struct bus *bus; |
| 568 | bus = &dev->link[link]; |
| 569 | if (bus->children) { |
| 570 | assign_resources(bus); |
| 571 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* set a default latency timer */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 575 | pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 576 | |
| 577 | /* set a default secondary latency timer */ |
| 578 | if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 579 | pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* zero the irq settings */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 583 | line = pci_read_config8(dev, PCI_INTERRUPT_PIN); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 584 | if (line) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 585 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 586 | } |
| 587 | /* set the cache line size, so far 64 bytes is good for everyone */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 588 | pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 591 | void pci_dev_enable_resources(struct device *dev) |
| 592 | { |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 593 | const struct pci_operations *ops; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 594 | uint16_t command; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 595 | |
| 596 | /* Set the subsystem vendor and device id for mainboard devices */ |
| 597 | ops = ops_pci(dev); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 598 | if (dev->on_mainboard && ops && ops->set_subsystem) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 599 | printk_debug("%s subsystem <- %02x/%02x\n", |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 600 | dev_path(dev), |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 601 | MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID, |
| 602 | MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 603 | ops->set_subsystem(dev, |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 604 | MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID, |
| 605 | MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID); |
| 606 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 607 | command = pci_read_config16(dev, PCI_COMMAND); |
| 608 | command |= dev->command; |
| 609 | printk_debug("%s cmd <- %02x\n", dev_path(dev), command); |
| 610 | pci_write_config16(dev, PCI_COMMAND, command); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void pci_bus_enable_resources(struct device *dev) |
| 614 | { |
| 615 | uint16_t ctrl; |
Li-Ta Lo | 515f6c7 | 2005-01-11 22:48:54 +0000 | [diff] [blame] | 616 | /* enable IO in command register if there is VGA card |
| 617 | * connected with (even it does not claim IO resource) */ |
| 618 | if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA) |
| 619 | dev->command |= PCI_COMMAND_IO; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 620 | ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL); |
| 621 | ctrl |= dev->link[0].bridge_ctrl; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 622 | ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 623 | printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl); |
| 624 | pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl); |
| 625 | |
| 626 | pci_dev_enable_resources(dev); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 627 | |
| 628 | enable_childrens_resources(dev); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 631 | void pci_bus_reset(struct bus *bus) |
| 632 | { |
| 633 | unsigned ctl; |
| 634 | ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL); |
| 635 | ctl |= PCI_BRIDGE_CTL_BUS_RESET; |
| 636 | pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl); |
| 637 | mdelay(10); |
| 638 | ctl &= ~PCI_BRIDGE_CTL_BUS_RESET; |
| 639 | pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl); |
| 640 | delay(1); |
| 641 | } |
| 642 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 643 | void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 644 | { |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 645 | pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 646 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 647 | } |
| 648 | |
Torsten Duwe | 1f2f800 | 2008-01-06 01:10:54 +0000 | [diff] [blame] | 649 | /** default handler: only runs the relevant pci bios. */ |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 650 | void pci_dev_init(struct device *dev) |
| 651 | { |
Torsten Duwe | 1f2f800 | 2008-01-06 01:10:54 +0000 | [diff] [blame] | 652 | #if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1 |
Stefan Reinauer | d98cf5b | 2008-08-01 11:25:41 +0000 | [diff] [blame] | 653 | void run_bios(struct device * dev, unsigned long addr); |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 654 | struct rom_header *rom, *ram; |
| 655 | |
Roman Kononov | 778a42b | 2007-04-06 18:34:39 +0000 | [diff] [blame] | 656 | #if CONFIG_PCI_ROM_RUN != 1 |
Torsten Duwe | 1f2f800 | 2008-01-06 01:10:54 +0000 | [diff] [blame] | 657 | /* We want to execute VGA option ROMs when CONFIG_VGA_ROM_RUN |
Roman Kononov | 778a42b | 2007-04-06 18:34:39 +0000 | [diff] [blame] | 658 | * is set but CONFIG_PCI_ROM_RUN is not. In this case we skip |
| 659 | * all other option ROM types. |
| 660 | */ |
Ronald Hoogenboom | 9b6b63e | 2008-02-28 23:10:38 +0000 | [diff] [blame] | 661 | if ((dev->class>>8)!=PCI_CLASS_DISPLAY_VGA) |
Roman Kononov | 778a42b | 2007-04-06 18:34:39 +0000 | [diff] [blame] | 662 | return; |
| 663 | #endif |
| 664 | |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 665 | rom = pci_rom_probe(dev); |
| 666 | if (rom == NULL) |
| 667 | return; |
Roman Kononov | 778a42b | 2007-04-06 18:34:39 +0000 | [diff] [blame] | 668 | |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 669 | ram = pci_rom_load(dev, rom); |
Yinghai Lu | 9e4faef | 2005-01-14 22:04:49 +0000 | [diff] [blame] | 670 | if (ram == NULL) |
| 671 | return; |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 672 | |
Stefan Reinauer | d98cf5b | 2008-08-01 11:25:41 +0000 | [diff] [blame] | 673 | run_bios(dev, (unsigned long)ram); |
Roman Kononov | 778a42b | 2007-04-06 18:34:39 +0000 | [diff] [blame] | 674 | |
| 675 | #if CONFIG_CONSOLE_VGA == 1 |
Torsten Duwe | 1f2f800 | 2008-01-06 01:10:54 +0000 | [diff] [blame] | 676 | /* vga_inited is a trigger of the VGA console code. */ |
Ronald Hoogenboom | 9b6b63e | 2008-02-28 23:10:38 +0000 | [diff] [blame] | 677 | if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA) { |
Torsten Duwe | 1f2f800 | 2008-01-06 01:10:54 +0000 | [diff] [blame] | 678 | extern int vga_inited; |
| 679 | vga_inited = 1; |
| 680 | } |
| 681 | #endif /* CONFIG_CONSOLE_VGA */ |
| 682 | #endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 683 | } |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 684 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 685 | /** Default device operation for PCI devices */ |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 686 | static struct pci_operations pci_dev_ops_pci = { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 687 | .set_subsystem = pci_dev_set_subsystem, |
| 688 | }; |
| 689 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 690 | struct device_operations default_pci_ops_dev = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 691 | .read_resources = pci_dev_read_resources, |
| 692 | .set_resources = pci_dev_set_resources, |
| 693 | .enable_resources = pci_dev_enable_resources, |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 694 | .init = pci_dev_init, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 695 | .scan_bus = 0, |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 696 | .enable = 0, |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 697 | .ops_pci = &pci_dev_ops_pci, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 698 | }; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 699 | |
| 700 | /** Default device operations for PCI bridges */ |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 701 | static struct pci_operations pci_bus_ops_pci = { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 702 | .set_subsystem = 0, |
| 703 | }; |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 704 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 705 | struct device_operations default_pci_ops_bus = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 706 | .read_resources = pci_bus_read_resources, |
| 707 | .set_resources = pci_dev_set_resources, |
| 708 | .enable_resources = pci_bus_enable_resources, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 709 | .init = 0, |
| 710 | .scan_bus = pci_scan_bridge, |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 711 | .enable = 0, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 712 | .reset_bus = pci_bus_reset, |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 713 | .ops_pci = &pci_bus_ops_pci, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 714 | }; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 715 | |
| 716 | /** |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 717 | * @brief Detect the type of downstream bridge |
| 718 | * |
| 719 | * This function is a heuristic to detect which type |
| 720 | * of bus is downstream of a pci to pci bridge. This |
| 721 | * functions by looking for various capability blocks |
| 722 | * to figure out the type of downstream bridge. PCI-X |
| 723 | * PCI-E, and Hypertransport all seem to have appropriate |
| 724 | * capabilities. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 725 | * |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 726 | * When only a PCI-Express capability is found the type |
| 727 | * is examined to see which type of bridge we have. |
| 728 | * |
| 729 | * @param dev |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 730 | * |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 731 | * @return appropriate bridge operations |
| 732 | */ |
| 733 | static struct device_operations *get_pci_bridge_ops(device_t dev) |
| 734 | { |
| 735 | unsigned pos; |
| 736 | |
| 737 | #if CONFIG_PCIX_PLUGIN_SUPPORT == 1 |
| 738 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
| 739 | if (pos) { |
| 740 | printk_debug("%s subbordinate bus PCI-X\n", dev_path(dev)); |
| 741 | return &default_pcix_ops_bus; |
| 742 | } |
| 743 | #endif |
| 744 | #if CONFIG_AGP_PLUGIN_SUPPORT == 1 |
| 745 | /* How do I detect an PCI to AGP bridge? */ |
| 746 | #endif |
| 747 | #if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1 |
| 748 | pos = 0; |
| 749 | while((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) { |
| 750 | unsigned flags; |
| 751 | flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS); |
| 752 | if ((flags >> 13) == 1) { |
| 753 | /* Host or Secondary Interface */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 754 | printk_debug("%s subbordinate bus Hypertransport\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 755 | dev_path(dev)); |
| 756 | return &default_ht_ops_bus; |
| 757 | } |
| 758 | } |
| 759 | #endif |
| 760 | #if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1 |
| 761 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIE); |
| 762 | if (pos) { |
| 763 | unsigned flags; |
| 764 | flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS); |
| 765 | switch((flags & PCI_EXP_FLAGS_TYPE) >> 4) { |
| 766 | case PCI_EXP_TYPE_ROOT_PORT: |
| 767 | case PCI_EXP_TYPE_UPSTREAM: |
| 768 | case PCI_EXP_TYPE_DOWNSTREAM: |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 769 | printk_debug("%s subbordinate bus PCI Express\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 770 | dev_path(dev)); |
| 771 | return &default_pciexp_ops_bus; |
| 772 | case PCI_EXP_TYPE_PCI_BRIDGE: |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 773 | printk_debug("%s subbordinate PCI\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 774 | dev_path(dev)); |
| 775 | return &default_pci_ops_bus; |
| 776 | default: |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | #endif |
| 781 | return &default_pci_ops_bus; |
| 782 | } |
| 783 | |
| 784 | /** |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 785 | * @brief Set up PCI device operation |
| 786 | * |
| 787 | * |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 788 | * @param dev |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 789 | * |
| 790 | * @see pci_drivers |
| 791 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 792 | static void set_pci_ops(struct device *dev) |
| 793 | { |
| 794 | struct pci_driver *driver; |
| 795 | if (dev->ops) { |
| 796 | return; |
| 797 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 798 | |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 799 | /* Look through the list of setup drivers and find one for |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 800 | * this pci device |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 801 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 802 | for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 803 | if ((driver->vendor == dev->vendor) && |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 804 | (driver->device == dev->device)) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 805 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 806 | dev->ops = driver->ops; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 807 | printk_spew("%s [%04x/%04x] %sops\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 808 | dev_path(dev), |
| 809 | driver->vendor, driver->device, |
| 810 | (driver->ops->scan_bus?"bus ":"")); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 811 | return; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 814 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 815 | /* If I don't have a specific driver use the default operations */ |
| 816 | switch(dev->hdr_type & 0x7f) { /* header type */ |
| 817 | case PCI_HEADER_TYPE_NORMAL: /* standard header */ |
| 818 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) |
| 819 | goto bad; |
| 820 | dev->ops = &default_pci_ops_dev; |
| 821 | break; |
| 822 | case PCI_HEADER_TYPE_BRIDGE: |
| 823 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 824 | goto bad; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 825 | dev->ops = get_pci_bridge_ops(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 826 | break; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 827 | #if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1 |
| 828 | case PCI_HEADER_TYPE_CARDBUS: |
| 829 | dev->ops = &default_cardbus_ops_bus; |
| 830 | break; |
| 831 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 832 | default: |
| 833 | bad: |
Li-Ta Lo | 69c5a90 | 2004-04-29 20:08:54 +0000 | [diff] [blame] | 834 | if (dev->enabled) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 835 | printk_err("%s [%04x/%04x/%06x] has unknown header " |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 836 | "type %02x, ignoring.\n", |
| 837 | dev_path(dev), |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 838 | dev->vendor, dev->device, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 839 | dev->class >> 8, dev->hdr_type); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 840 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 841 | } |
| 842 | return; |
| 843 | } |
| 844 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 845 | |
| 846 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 847 | /** |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 848 | * @brief See if we have already allocated a device structure for a given devfn. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 849 | * |
| 850 | * Given a linked list of PCI device structures and a devfn number, find the |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 851 | * device structure correspond to the devfn, if present. This function also |
| 852 | * removes the device structure from the linked list. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 853 | * |
| 854 | * @param list the device structure list |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 855 | * @param devfn a device/function number |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 856 | * |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 857 | * @return pointer to the device structure found or null of we have not |
| 858 | * allocated a device for this devfn yet. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 859 | */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 860 | static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 861 | { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 862 | struct device *dev; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 863 | dev = 0; |
| 864 | for(; *list; list = &(*list)->sibling) { |
Eric Biederman | ad1b35a | 2003-10-14 02:36:51 +0000 | [diff] [blame] | 865 | if ((*list)->path.type != DEVICE_PATH_PCI) { |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 866 | printk_err("child %s not a pci device\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 867 | dev_path(*list)); |
Eric Biederman | ad1b35a | 2003-10-14 02:36:51 +0000 | [diff] [blame] | 868 | continue; |
| 869 | } |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 870 | if ((*list)->path.pci.devfn == devfn) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 871 | /* Unlink from the list */ |
| 872 | dev = *list; |
| 873 | *list = (*list)->sibling; |
| 874 | dev->sibling = 0; |
| 875 | break; |
| 876 | } |
| 877 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 878 | /* Just like alloc_dev add the device to the list of device on the bus. |
| 879 | * When the list of devices was formed we removed all of the parents |
| 880 | * children, and now we are interleaving static and dynamic devices in |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 881 | * order on the bus. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 882 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 883 | if (dev) { |
| 884 | device_t child; |
| 885 | /* Find the last child of our parent */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 886 | for(child = dev->bus->children; child && child->sibling; ) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 887 | child = child->sibling; |
| 888 | } |
| 889 | /* Place the device on the list of children of it's parent. */ |
| 890 | if (child) { |
| 891 | child->sibling = dev; |
| 892 | } else { |
| 893 | dev->bus->children = dev; |
| 894 | } |
| 895 | } |
| 896 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 897 | return dev; |
| 898 | } |
| 899 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 900 | /** |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 901 | * @brief Scan a PCI bus. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 902 | * |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 903 | * Determine the existence of a given PCI device. |
| 904 | * |
| 905 | * @param bus pointer to the bus structure |
| 906 | * @param devfn to look at |
| 907 | * |
| 908 | * @return The device structure for hte device (if found) |
| 909 | * or the NULL if no device is found. |
| 910 | */ |
| 911 | device_t pci_probe_dev(device_t dev, struct bus *bus, unsigned devfn) |
| 912 | { |
| 913 | uint32_t id, class; |
| 914 | uint8_t hdr_type; |
| 915 | |
| 916 | /* Detect if a device is present */ |
| 917 | if (!dev) { |
| 918 | struct device dummy; |
| 919 | dummy.bus = bus; |
| 920 | dummy.path.type = DEVICE_PATH_PCI; |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 921 | dummy.path.pci.devfn = devfn; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 922 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
| 923 | /* Have we found somthing? |
| 924 | * Some broken boards return 0 if a slot is empty. |
| 925 | */ |
| 926 | if ( (id == 0xffffffff) || (id == 0x00000000) || |
| 927 | (id == 0x0000ffff) || (id == 0xffff0000)) |
| 928 | { |
Stefan Reinauer | f657d75 | 2008-09-11 06:52:22 +0000 | [diff] [blame] | 929 | printk_spew("%s, bad id 0x%x\n", dev_path(&dummy), id); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 930 | return NULL; |
| 931 | } |
| 932 | dev = alloc_dev(bus, &dummy.path); |
| 933 | } |
| 934 | else { |
| 935 | /* Enable/disable the device. Once we have |
| 936 | * found the device specific operations this |
| 937 | * operations we will disable the device with |
| 938 | * those as well. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 939 | * |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 940 | * This is geared toward devices that have subfunctions |
| 941 | * that do not show up by default. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 942 | * |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 943 | * If a device is a stuff option on the motherboard |
| 944 | * it may be absent and enable_dev must cope. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 945 | * |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 946 | */ |
| 947 | /* Run the magice enable sequence for the device */ |
| 948 | if (dev->chip_ops && dev->chip_ops->enable_dev) { |
| 949 | dev->chip_ops->enable_dev(dev); |
| 950 | } |
| 951 | /* Now read the vendor and device id */ |
| 952 | id = pci_read_config32(dev, PCI_VENDOR_ID); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 953 | |
| 954 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 955 | /* If the device does not have a pci id disable it. |
| 956 | * Possibly this is because we have already disabled |
| 957 | * the device. But this also handles optional devices |
| 958 | * that may not always show up. |
| 959 | */ |
| 960 | /* If the chain is fully enumerated quit */ |
| 961 | if ( (id == 0xffffffff) || (id == 0x00000000) || |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 962 | (id == 0x0000ffff) || (id == 0xffff0000)) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 963 | { |
| 964 | if (dev->enabled) { |
| 965 | printk_info("Disabling static device: %s\n", |
| 966 | dev_path(dev)); |
| 967 | dev->enabled = 0; |
| 968 | } |
| 969 | return dev; |
| 970 | } |
| 971 | } |
| 972 | /* Read the rest of the pci configuration information */ |
| 973 | hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 974 | class = pci_read_config32(dev, PCI_CLASS_REVISION); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 975 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 976 | /* Store the interesting information in the device structure */ |
| 977 | dev->vendor = id & 0xffff; |
| 978 | dev->device = (id >> 16) & 0xffff; |
| 979 | dev->hdr_type = hdr_type; |
| 980 | /* class code, the upper 3 bytes of PCI_CLASS_REVISION */ |
| 981 | dev->class = class >> 8; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 982 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 983 | |
| 984 | /* Architectural/System devices always need to |
| 985 | * be bus masters. |
| 986 | */ |
| 987 | if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) { |
| 988 | dev->command |= PCI_COMMAND_MASTER; |
| 989 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 990 | /* Look at the vendor and device id, or at least the |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 991 | * header type and class and figure out which set of |
| 992 | * configuration methods to use. Unless we already |
| 993 | * have some pci ops. |
| 994 | */ |
| 995 | set_pci_ops(dev); |
| 996 | |
| 997 | /* Now run the magic enable/disable sequence for the device */ |
| 998 | if (dev->ops && dev->ops->enable) { |
| 999 | dev->ops->enable(dev); |
| 1000 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1001 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1002 | |
| 1003 | /* Display the device and error if we don't have some pci operations |
| 1004 | * for it. |
| 1005 | */ |
| 1006 | printk_debug("%s [%04x/%04x] %s%s\n", |
| 1007 | dev_path(dev), |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1008 | dev->vendor, dev->device, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1009 | dev->enabled?"enabled": "disabled", |
| 1010 | dev->ops?"" : " No operations" |
| 1011 | ); |
| 1012 | |
| 1013 | return dev; |
| 1014 | } |
| 1015 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1016 | /** |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1017 | * @brief Scan a PCI bus. |
| 1018 | * |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1019 | * Determine the existence of devices and bridges on a PCI bus. If there are |
| 1020 | * bridges on the bus, recursively scan the buses behind the bridges. |
| 1021 | * |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1022 | * This function is the default scan_bus() method for the root device |
| 1023 | * 'dev_root'. |
| 1024 | * |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1025 | * @param bus pointer to the bus structure |
| 1026 | * @param min_devfn minimum devfn to look at in the scan usually 0x00 |
| 1027 | * @param max_devfn maximum devfn to look at in the scan usually 0xff |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1028 | * @param max current bus number |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1029 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1030 | * @return The maximum bus number found, after scanning all subordinate busses |
| 1031 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1032 | unsigned int pci_scan_bus(struct bus *bus, |
| 1033 | unsigned min_devfn, unsigned max_devfn, |
| 1034 | unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1035 | { |
| 1036 | unsigned int devfn; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1037 | device_t old_devices; |
| 1038 | device_t child; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1039 | |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 1040 | #if PCI_BUS_SEGN_BITS |
| 1041 | printk_debug("PCI: pci_scan_bus for bus %04x:%02x\n", bus->secondary >> 8, bus->secondary & 0xff); |
| 1042 | #else |
| 1043 | printk_debug("PCI: pci_scan_bus for bus %02x\n", bus->secondary); |
| 1044 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1045 | |
| 1046 | old_devices = bus->children; |
| 1047 | bus->children = 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1048 | |
| 1049 | post_code(0x24); |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame] | 1050 | /* probe all devices/functions on this bus with some optimization for |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1051 | * non-existence and single funcion devices |
| 1052 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1053 | for (devfn = min_devfn; devfn <= max_devfn; devfn++) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1054 | device_t dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1055 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 1056 | /* First thing setup the device structure */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1057 | dev = pci_scan_get_dev(&old_devices, devfn); |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame] | 1058 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1059 | /* See if a device is present and setup the device |
| 1060 | * structure. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 1061 | */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1062 | dev = pci_probe_dev(dev, bus, devfn); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 1063 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1064 | /* if this is not a multi function device, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1065 | * or the device is not present don't waste |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1066 | * time probing another function. |
| 1067 | * Skip to next device. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1068 | */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1069 | if ((PCI_FUNC(devfn) == 0x00) && |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1070 | (!dev || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) |
| 1071 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1072 | devfn += 0x07; |
| 1073 | } |
| 1074 | } |
| 1075 | post_code(0x25); |
| 1076 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1077 | /* Die if any left over static devices are are found. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1078 | * There's probably a problem in the Config.lb. |
| 1079 | */ |
| 1080 | if(old_devices) { |
| 1081 | device_t left; |
| 1082 | for(left = old_devices; left; left = left->sibling) { |
Ronald G. Minnich | e800b91 | 2006-01-17 21:12:03 +0000 | [diff] [blame] | 1083 | printk_err("%s\n", dev_path(left)); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1084 | } |
Stefan Reinauer | d98cf5b | 2008-08-01 11:25:41 +0000 | [diff] [blame] | 1085 | printk_warning("PCI: Left over static devices. Check your mainboard Config.lb\n"); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1088 | /* For all children that implement scan_bus (i.e. bridges) |
| 1089 | * scan the bus behind that child. |
| 1090 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1091 | for(child = bus->children; child; child = child->sibling) { |
| 1092 | max = scan_bus(child, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1093 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1094 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1095 | /* |
| 1096 | * We've scanned the bus and so we know all about what's on |
| 1097 | * the other side of any bridges that may be on this bus plus |
| 1098 | * any devices. |
| 1099 | * |
| 1100 | * Return how far we've got finding sub-buses. |
| 1101 | */ |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 1102 | printk_debug("PCI: pci_scan_bus returning with max=%03x\n", max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1103 | post_code(0x55); |
| 1104 | return max; |
| 1105 | } |
| 1106 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1107 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1108 | /** |
| 1109 | * @brief Scan a PCI bridge and the buses behind the bridge. |
| 1110 | * |
| 1111 | * Determine the existence of buses behind the bridge. Set up the bridge |
| 1112 | * according to the result of the scan. |
| 1113 | * |
| 1114 | * This function is the default scan_bus() method for PCI bridge devices. |
| 1115 | * |
| 1116 | * @param dev pointer to the bridge device |
| 1117 | * @param max the highest bus number assgined up to now |
| 1118 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1119 | * @return The maximum bus number found, after scanning all subordinate busses |
| 1120 | */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1121 | unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max, |
| 1122 | unsigned int (*do_scan_bus)(struct bus *bus, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1123 | unsigned min_devfn, unsigned max_devfn, unsigned int max)) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1124 | { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1125 | struct bus *bus; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1126 | uint32_t buses; |
| 1127 | uint16_t cr; |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 1128 | |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 1129 | printk_spew("%s for %s\n", __func__, dev_path(dev)); |
| 1130 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1131 | bus = &dev->link[0]; |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 1132 | bus->dev = dev; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1133 | dev->links = 1; |
| 1134 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1135 | /* Set up the primary, secondary and subordinate bus numbers. We have |
| 1136 | * no idea how many buses are behind this bridge yet, so we set the |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1137 | * subordinate bus number to 0xff for the moment. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1138 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1139 | bus->secondary = ++max; |
| 1140 | bus->subordinate = 0xff; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1141 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1142 | /* Clear all status bits and turn off memory, I/O and master enables. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1143 | cr = pci_read_config16(dev, PCI_COMMAND); |
| 1144 | pci_write_config16(dev, PCI_COMMAND, 0x0000); |
| 1145 | pci_write_config16(dev, PCI_STATUS, 0xffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1146 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1147 | /* |
| 1148 | * Read the existing primary/secondary/subordinate bus |
| 1149 | * number configuration. |
| 1150 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1151 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1152 | |
| 1153 | /* Configure the bus numbers for this bridge: the configuration |
| 1154 | * transactions will not be propagated by the bridge if it is not |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1155 | * correctly configured. |
| 1156 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1157 | buses &= 0xff000000; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1158 | buses |= (((unsigned int) (dev->bus->secondary) << 0) | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1159 | ((unsigned int) (bus->secondary) << 8) | |
| 1160 | ((unsigned int) (bus->subordinate) << 16)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1161 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 1162 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1163 | /* Now we can scan all subordinate buses |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1164 | * i.e. the bus behind the bridge. |
| 1165 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1166 | max = do_scan_bus(bus, 0x00, 0xff, max); |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 1167 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1168 | /* We know the number of buses behind this bridge. Set the subordinate |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1169 | * bus number to its real value. |
| 1170 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1171 | bus->subordinate = max; |
| 1172 | buses = (buses & 0xff00ffff) | |
| 1173 | ((unsigned int) (bus->subordinate) << 16); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 1174 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
| 1175 | pci_write_config16(dev, PCI_COMMAND, cr); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1176 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1177 | printk_spew("%s returns max %d\n", __func__, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1178 | return max; |
| 1179 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1180 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1181 | /** |
| 1182 | * @brief Scan a PCI bridge and the buses behind the bridge. |
| 1183 | * |
| 1184 | * Determine the existence of buses behind the bridge. Set up the bridge |
| 1185 | * according to the result of the scan. |
| 1186 | * |
| 1187 | * This function is the default scan_bus() method for PCI bridge devices. |
| 1188 | * |
| 1189 | * @param dev pointer to the bridge device |
| 1190 | * @param max the highest bus number assgined up to now |
| 1191 | * |
| 1192 | * @return The maximum bus number found, after scanning all subordinate busses |
| 1193 | */ |
| 1194 | unsigned int pci_scan_bridge(struct device *dev, unsigned int max) |
| 1195 | { |
| 1196 | return do_pci_scan_bridge(dev, max, pci_scan_bus); |
| 1197 | } |
| 1198 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | Tell the EISA int controller this int must be level triggered |
| 1201 | THIS IS A KLUDGE -- sorry, this needs to get cleaned up. |
| 1202 | */ |
Ronald G. Minnich | 88fb1a6 | 2006-06-22 04:37:27 +0000 | [diff] [blame] | 1203 | void pci_level_irq(unsigned char intNum) |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1204 | { |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 1205 | unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1206 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1207 | printk_spew("%s: current ints are 0x%x\n", __func__, intBits); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1208 | intBits |= (1 << intNum); |
| 1209 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1210 | printk_spew("%s: try to set ints 0x%x\n", __func__, intBits); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 1211 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1212 | // Write new values |
| 1213 | outb((unsigned char) intBits, 0x4d0); |
| 1214 | outb((unsigned char) (intBits >> 8), 0x4d1); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 1215 | |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 1216 | /* this seems like an error but is not ... */ |
Ronald G. Minnich | 02fa3b2 | 2004-10-06 17:33:54 +0000 | [diff] [blame] | 1217 | #if 1 |
Ronald G. Minnich | 2cf779d | 2006-09-18 22:50:51 +0000 | [diff] [blame] | 1218 | if (inb(0x4d0) != (intBits & 0xff)) { |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 1219 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
Ronald G. Minnich | 2cf779d | 2006-09-18 22:50:51 +0000 | [diff] [blame] | 1220 | __func__, intBits &0xff, inb(0x4d0)); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 1221 | } |
Ronald G. Minnich | 2cf779d | 2006-09-18 22:50:51 +0000 | [diff] [blame] | 1222 | if (inb(0x4d1) != ((intBits >> 8) & 0xff)) { |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 1223 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
Ronald G. Minnich | 2cf779d | 2006-09-18 22:50:51 +0000 | [diff] [blame] | 1224 | __func__, (intBits>>8) &0xff, inb(0x4d1)); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 1225 | } |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 1226 | #endif |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1229 | /* |
| 1230 | This function assigns IRQs for all functions contained within |
| 1231 | the indicated device address. If the device does not exist or does |
| 1232 | not require interrupts then this function has no effect. |
| 1233 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1234 | This function should be called for each PCI slot in your system. |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1235 | |
| 1236 | pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1237 | this slot. |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1238 | The particular irq #s that are passed in depend on the routing inside |
| 1239 | your southbridge and on your motherboard. |
| 1240 | |
| 1241 | -kevinh@ispiri.com |
| 1242 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 1243 | void pci_assign_irqs(unsigned bus, unsigned slot, |
| 1244 | const unsigned char pIntAtoD[4]) |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1245 | { |
| 1246 | unsigned functNum; |
| 1247 | device_t pdev; |
| 1248 | unsigned char line; |
| 1249 | unsigned char irq; |
| 1250 | unsigned char readback; |
| 1251 | |
| 1252 | /* Each slot may contain up to eight functions */ |
| 1253 | for (functNum = 0; functNum < 8; functNum++) { |
| 1254 | pdev = dev_find_slot(bus, (slot << 3) + functNum); |
| 1255 | |
| 1256 | if (pdev) { |
| 1257 | line = pci_read_config8(pdev, PCI_INTERRUPT_PIN); |
| 1258 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame^] | 1259 | // PCI spec says all other values are reserved |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 1260 | if ((line >= 1) && (line <= 4)) { |
| 1261 | irq = pIntAtoD[line - 1]; |
| 1262 | |
| 1263 | printk_debug("Assigning IRQ %d to %d:%x.%d\n", \ |
| 1264 | irq, bus, slot, functNum); |
| 1265 | |
| 1266 | pci_write_config8(pdev, PCI_INTERRUPT_LINE,\ |
| 1267 | pIntAtoD[line - 1]); |
| 1268 | |
| 1269 | readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE); |
| 1270 | printk_debug(" Readback = %d\n", readback); |
| 1271 | |
| 1272 | // Change to level triggered |
| 1273 | pci_level_irq(pIntAtoD[line - 1]); |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | } |