blob: 2a7502b2804a0be672da3f7a6f9a4cc60d58e27f [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
2 * PCI Bus Services, see include/linux/pci.h for further explanation.
3 *
4 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5 * David Mosberger-Tang
6 *
7 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 *
9 * Copyright 2003 -- Eric Biederman <ebiederman@lnxi.com>
10 */
11
12#include <console/console.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <bitops.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000016#include <string.h>
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +000017#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000018#include <device/device.h>
19#include <device/pci.h>
20#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000021#include <part/hard_reset.h>
Eric Biederman30e143a2003-09-01 23:45:32 +000022#include <part/fallback_boot.h>
Eric Biederman03acab62004-10-14 21:25:53 +000023#include <delay.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000024#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
25#include <device/hypertransport.h>
26#endif
27#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
28#include <device/pcix.h>
29#endif
30#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
31#include <device/pciexp.h>
32#endif
33#if CONFGI_AGP_PLUGIN_SUPPORT == 1
34#include <device/agp.h>
35#endif
36#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
37#include <device/cardbus.h>
38#endif
Eric Biederman03acab62004-10-14 21:25:53 +000039
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000040uint8_t pci_moving_config8(struct device *dev, unsigned reg)
Eric Biederman03acab62004-10-14 21:25:53 +000041{
42 uint8_t value, ones, zeroes;
43 value = pci_read_config8(dev, reg);
44
45 pci_write_config8(dev, reg, 0xff);
46 ones = pci_read_config8(dev, reg);
47
48 pci_write_config8(dev, reg, 0x00);
49 zeroes = pci_read_config8(dev, reg);
50
51 pci_write_config8(dev, reg, value);
52
53 return ones ^ zeroes;
54}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000055
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000056uint16_t pci_moving_config16(struct device *dev, unsigned reg)
Eric Biederman03acab62004-10-14 21:25:53 +000057{
58 uint16_t value, ones, zeroes;
59 value = pci_read_config16(dev, reg);
60
61 pci_write_config16(dev, reg, 0xffff);
62 ones = pci_read_config16(dev, reg);
63
64 pci_write_config16(dev, reg, 0x0000);
65 zeroes = pci_read_config16(dev, reg);
66
67 pci_write_config16(dev, reg, value);
68
69 return ones ^ zeroes;
70}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000071
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000072uint32_t pci_moving_config32(struct device *dev, unsigned reg)
Eric Biederman03acab62004-10-14 21:25:53 +000073{
74 uint32_t value, ones, zeroes;
75 value = pci_read_config32(dev, reg);
76
77 pci_write_config32(dev, reg, 0xffffffff);
78 ones = pci_read_config32(dev, reg);
79
80 pci_write_config32(dev, reg, 0x00000000);
81 zeroes = pci_read_config32(dev, reg);
82
83 pci_write_config32(dev, reg, value);
84
85 return ones ^ zeroes;
86}
87
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000088unsigned pci_find_next_capability(device_t dev, unsigned cap, unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +000089{
90 unsigned pos;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000091 unsigned status;
92 unsigned reps = 48;
Eric Biederman03acab62004-10-14 21:25:53 +000093 pos = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000094 status = pci_read_config16(dev, PCI_STATUS);
95 if (!(status & PCI_STATUS_CAP_LIST)) {
96 return 0;
97 }
Eric Biederman03acab62004-10-14 21:25:53 +000098 switch(dev->hdr_type & 0x7f) {
99 case PCI_HEADER_TYPE_NORMAL:
100 case PCI_HEADER_TYPE_BRIDGE:
101 pos = PCI_CAPABILITY_LIST;
102 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000103 case PCI_HEADER_TYPE_CARDBUS:
104 pos = PCI_CB_CAPABILITY_LIST;
105 break;
106 default:
107 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000108 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000109 pos = pci_read_config8(dev, pos);
110 while(reps-- && (pos >= 0x40)) { /* loop through the linked list */
Eric Biederman03acab62004-10-14 21:25:53 +0000111 int this_cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000112 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000113 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000114 printk_spew("Capability: 0x%02x @ 0x%02x\n", cap, pos);
115 if (this_cap == 0xff) {
116 break;
117 }
118 if (!last && (this_cap == cap)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000119 return pos;
120 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000121 if (last == pos) {
122 last = 0;
123 }
124 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000125 }
126 return 0;
127}
128
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000129unsigned pci_find_capability(device_t dev, unsigned cap)
130{
131 return pci_find_next_capability(dev, cap, 0);
132
133}
134
Eric Biederman8ca8d762003-04-22 19:02:15 +0000135/** Given a device and register, read the size of the BAR for that register.
136 * @param dev Pointer to the device structure
137 * @param resource Pointer to the resource structure
138 * @param index Address of the pci configuration register
139 */
Eric Biederman03acab62004-10-14 21:25:53 +0000140struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000141{
Eric Biederman5cd81732004-03-11 15:01:31 +0000142 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000143 unsigned long value, attr;
144 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000145
146 /* Initialize the resources to nothing */
Eric Biederman03acab62004-10-14 21:25:53 +0000147 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000148
Eric Biederman03acab62004-10-14 21:25:53 +0000149 /* Get the initial value */
150 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151
Eric Biederman03acab62004-10-14 21:25:53 +0000152 /* See which bits move */
153 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000154
Eric Biederman03acab62004-10-14 21:25:53 +0000155 /* Initialize attr to the bits that do not move */
156 attr = value & ~moving;
157
158 /* If it is a 64bit resource look at the high half as well */
159 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000160 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) == PCI_BASE_ADDRESS_MEM_LIMIT_64))
Eric Biederman03acab62004-10-14 21:25:53 +0000161 {
162 /* Find the high bits that move */
163 moving |= ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
164 }
165 /* Find the resource constraints.
166 *
167 * Start by finding the bits that move. From there:
168 * - Size is the least significant bit of the bits that move.
169 * - Limit is all of the bits that move plus all of the lower bits.
170 * See PCI Spec 6.2.5.1 ...
Eric Biederman8ca8d762003-04-22 19:02:15 +0000171 */
Eric Biederman03acab62004-10-14 21:25:53 +0000172 limit = 0;
173 if (moving) {
174 resource->size = 1;
175 resource->align = resource->gran = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000176 while(!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000177 resource->size <<= 1;
178 resource->align += 1;
179 resource->gran += 1;
180 }
181 resource->limit = limit = moving | (resource->size - 1);
182 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183 /*
184 * some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000185 * really size correctly.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186 * Example: the acer m7229 has BARs 1-4 normally read-only.
187 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
188 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
189 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000190 * We catch this case and ignore it by observing which bits move,
191 * This also catches the common case unimplemented registers
192 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000193 */
Eric Biederman03acab62004-10-14 21:25:53 +0000194 if (moving == 0) {
195 if (value != 0) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000196 printk_debug(
197 "%s register %02x(%08x), read-only ignoring it\n",
198 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199 }
200 resource->flags = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000201 }
202 else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000203 /* An I/O mapped base address */
204 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000205 resource->flags |= IORESOURCE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000206 /* I don't want to deal with 32bit I/O resources */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000207 resource->limit = 0xffff;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000208 }
209 else {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000210 /* A Memory mapped base address */
Eric Biederman03acab62004-10-14 21:25:53 +0000211 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000212 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000213 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000214 resource->flags |= IORESOURCE_PREFETCH;
215 }
Eric Biederman03acab62004-10-14 21:25:53 +0000216 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
217 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000218 /* 32bit limit */
219 resource->limit = 0xffffffffUL;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000220 }
221 else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222 /* 1MB limit */
223 resource->limit = 0x000fffffUL;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000224 }
225 else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000226 /* 64bit limit */
227 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000228 resource->flags |= IORESOURCE_PCI64;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000229 }
230 else {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231 /* Invalid value */
232 resource->flags = 0;
233 }
234 }
Eric Biederman03acab62004-10-14 21:25:53 +0000235 /* Don't let the limit exceed which bits can move */
236 if (resource->limit > limit) {
237 resource->limit = limit;
238 }
239#if 0
240 if (resource->flags) {
241 printk_debug("%s %02x ->",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000242 dev_path(dev), resource->index);
Eric Biederman03acab62004-10-14 21:25:53 +0000243 printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000244 value, zeroes, ones, attr);
Eric Biederman03acab62004-10-14 21:25:53 +0000245 printk_debug(
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000246 "%s %02x -> size: 0x%08Lx max: 0x%08Lx %s\n ",
Eric Biederman03acab62004-10-14 21:25:53 +0000247 dev_path(dev),
248 resource->index,
249 resource->size, resource->limit,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000250 resource_type(resource));
Eric Biederman03acab62004-10-14 21:25:53 +0000251 }
252#endif
253
Eric Biederman5cd81732004-03-11 15:01:31 +0000254 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000255}
256
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000257static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000258{
259 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000260 unsigned long value;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000261 resource_t moving, limit;
262
Li-Ta Lobec039c2005-01-19 23:19:26 +0000263 if ((dev->on_mainboard) && (dev->rom_address == 0)) {
264 //skip it if rom_address is not set in MB Config.lb
Yinghai Lubcde1612005-01-14 05:34:09 +0000265 return;
266 }
267
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000268 /* Initialize the resources to nothing */
269 resource = new_resource(dev, index);
270
271 /* Get the initial value */
272 value = pci_read_config32(dev, index);
273
274 /* See which bits move */
275 moving = pci_moving_config32(dev, index);
276 /* clear the Enable bit */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000277 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000278
279 /* Find the resource constraints.
280 *
281 * Start by finding the bits that move. From there:
282 * - Size is the least significant bit of the bits that move.
283 * - Limit is all of the bits that move plus all of the lower bits.
284 * See PCI Spec 6.2.5.1 ...
285 */
286 limit = 0;
287
288 if (moving) {
289 resource->size = 1;
290 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000291 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000292 resource->size <<= 1;
293 resource->align += 1;
294 resource->gran += 1;
295 }
296 resource->limit = limit = moving | (resource->size - 1);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000297 }
298
299 if (moving == 0) {
300 if (value != 0) {
301 printk_debug("%s register %02x(%08x), read-only ignoring it\n",
302 dev_path(dev), index, value);
303 }
304 resource->flags = 0;
305 } else {
306 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
307 }
Yinghai Luc7870ac2005-01-13 19:14:52 +0000308
309 /* for on board device with embedded ROM image, the ROM image is at
310 * fixed address specified in the Config.lb, the dev->rom_address is
311 * inited by driver_pci_onboard_ops::enable_dev() */
Yinghai Lubcde1612005-01-14 05:34:09 +0000312 if ((dev->on_mainboard) && (dev->rom_address != 0)) {
Yinghai Luc7870ac2005-01-13 19:14:52 +0000313 resource->base = dev->rom_address;
314 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
315 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
316 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000317
318 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000319}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000320
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000321/** Read the base address registers for a given device.
322 * @param dev Pointer to the dev structure
323 * @param howmany How many registers to read (6 for device, 2 for bridge)
324 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000325static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000326{
327 unsigned long index;
328
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000329 for(index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000330 struct resource *resource;
331 resource = pci_get_resource(dev, index);
332 index += (resource->flags & IORESOURCE_PCI64)?8:4;
333 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000334
335 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000336}
337
Eric Biederman03acab62004-10-14 21:25:53 +0000338static void pci_set_resource(struct device *dev, struct resource *resource);
339
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000340static void pci_record_bridge_resource(
341 struct device *dev, resource_t moving,
342 unsigned index, unsigned long mask, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000343{
344 /* Initiliaze the constraints on the current bus */
345 struct resource *resource;
346 resource = 0;
347 if (moving) {
348 unsigned long gran;
349 resource_t step;
350 resource = new_resource(dev, index);
351 resource->size = 0;
352 gran = 0;
353 step = 1;
354 while((moving & step) == 0) {
355 gran += 1;
356 step <<= 1;
357 }
358 resource->gran = gran;
359 resource->align = gran;
360 resource->limit = moving | (step - 1);
361 resource->flags = type | IORESOURCE_PCI_BRIDGE;
362 compute_allocate_resource(&dev->link[0], resource, mask, type);
363 /* If there is nothing behind the resource,
364 * clear it and forget it.
365 */
366 if (resource->size == 0) {
367 resource->base = moving;
368 resource->flags |= IORESOURCE_ASSIGNED;
369 resource->flags &= ~IORESOURCE_STORED;
370 pci_set_resource(dev, resource);
371 resource->flags = 0;
372 }
373 }
374 return;
375}
376
Eric Biederman8ca8d762003-04-22 19:02:15 +0000377static void pci_bridge_read_bases(struct device *dev)
378{
Eric Biederman03acab62004-10-14 21:25:53 +0000379 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000380
Eric Biederman03acab62004-10-14 21:25:53 +0000381 /* See if the bridge I/O resources are implemented */
382 moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
383 moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
384
385 moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
386 moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
387
388 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000389
390 /* Initialize the io space constraints on the current bus */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000391 pci_record_bridge_resource(
392 dev, moving, PCI_IO_BASE,
393 IORESOURCE_IO, IORESOURCE_IO);
394
Eric Biederman03acab62004-10-14 21:25:53 +0000395
396 /* See if the bridge prefmem resources are implemented */
397 moving_base = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
398 moving_base |= ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
399
400 moving_limit = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
401 moving_limit |= ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
402
403 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000404 /* Initiliaze the prefetchable memory constraints on the current bus */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000405 pci_record_bridge_resource(
406 dev, moving, PCI_PREF_MEMORY_BASE,
407 IORESOURCE_MEM | IORESOURCE_PREFETCH,
408 IORESOURCE_MEM | IORESOURCE_PREFETCH);
409
Eric Biederman03acab62004-10-14 21:25:53 +0000410
411 /* See if the bridge mem resources are implemented */
412 moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
413 moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
414
415 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000416
417 /* Initialize the memory resources on the current bus */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000418 pci_record_bridge_resource(
419 dev, moving, PCI_MEMORY_BASE,
420 IORESOURCE_MEM | IORESOURCE_PREFETCH,
421 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000422
Eric Biederman5cd81732004-03-11 15:01:31 +0000423 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000424}
425
Eric Biederman5899fd82003-04-24 06:25:08 +0000426void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000427{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000428 pci_read_bases(dev, 6);
429 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000430}
431
Eric Biederman5899fd82003-04-24 06:25:08 +0000432void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000433{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000434 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000435 pci_read_bases(dev, 2);
436 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000437}
438
Eric Biederman8ca8d762003-04-22 19:02:15 +0000439static void pci_set_resource(struct device *dev, struct resource *resource)
440{
Eric Biederman03acab62004-10-14 21:25:53 +0000441 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000442
Eric Biederman8ca8d762003-04-22 19:02:15 +0000443 /* Make certain the resource has actually been set */
Eric Biederman5cd81732004-03-11 15:01:31 +0000444 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000445 printk_err("ERROR: %s %02x %s size: 0x%010Lx not assigned\n",
446 dev_path(dev), resource->index,
447 resource_type(resource),
448 resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000449 return;
450 }
451
Eric Biederman5cd81732004-03-11 15:01:31 +0000452 /* If I have already stored this resource don't worry about it */
453 if (resource->flags & IORESOURCE_STORED) {
454 return;
455 }
456
Eric Biederman03acab62004-10-14 21:25:53 +0000457 /* If the resources is substractive don't worry about it */
458 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
459 return;
460 }
461
Eric Biederman8ca8d762003-04-22 19:02:15 +0000462 /* Only handle PCI memory and IO resources for now */
463 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
464 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000465
Eric Biederman03acab62004-10-14 21:25:53 +0000466 /* Enable the resources in the command register */
467 if (resource->size) {
468 if (resource->flags & IORESOURCE_MEM) {
469 dev->command |= PCI_COMMAND_MEMORY;
470 }
471 if (resource->flags & IORESOURCE_IO) {
472 dev->command |= PCI_COMMAND_IO;
473 }
474 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
475 dev->command |= PCI_COMMAND_MASTER;
476 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000477 }
478 /* Get the base address */
479 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000480
Eric Biederman03acab62004-10-14 21:25:53 +0000481 /* Get the end */
482 end = resource_end(resource);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000483
Eric Biederman5cd81732004-03-11 15:01:31 +0000484 /* Now store the resource */
485 resource->flags |= IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000486 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000487 unsigned long base_lo, base_hi;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000488 /*
489 * some chipsets allow us to set/clear the IO bit.
490 * (e.g. VIA 82c686a.) So set it to be safe)
491 */
Eric Biederman03acab62004-10-14 21:25:53 +0000492 base_lo = base & 0xffffffff;
493 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000494 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000495 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000496 }
Eric Biederman03acab62004-10-14 21:25:53 +0000497 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000498 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000499 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000500 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000501 }
502 else if (resource->index == PCI_IO_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000503 /* set the IO ranges */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000504 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000505 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman03acab62004-10-14 21:25:53 +0000506 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
507 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
508 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
509 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000510 }
511 else if (resource->index == PCI_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000512 /* set the memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000513 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000514 IORESOURCE_MEM | IORESOURCE_PREFETCH,
515 IORESOURCE_MEM);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000516 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000517 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000518 }
519 else if (resource->index == PCI_PREF_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000520 /* set the prefetchable memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000521 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000522 IORESOURCE_MEM | IORESOURCE_PREFETCH,
523 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000524 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
525 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
526 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
527 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000528 }
529 else {
Eric Biederman5cd81732004-03-11 15:01:31 +0000530 /* Don't let me think I stored the resource */
531 resource->flags &= ~IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000532 printk_err("ERROR: invalid resource->index %x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000533 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000534 }
Eric Biederman03acab62004-10-14 21:25:53 +0000535 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000536 return;
537}
538
Eric Biederman5899fd82003-04-24 06:25:08 +0000539void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000540{
541 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000542 unsigned link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000543 uint8_t line;
544
545 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000546
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000547 for(resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000548 pci_set_resource(dev, resource);
549 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000550 for(link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000551 struct bus *bus;
552 bus = &dev->link[link];
553 if (bus->children) {
554 assign_resources(bus);
555 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000556 }
557
558 /* set a default latency timer */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000559 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000560
561 /* set a default secondary latency timer */
562 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000563 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000564 }
565
566 /* zero the irq settings */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000567 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000568 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000569 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000570 }
571 /* set the cache line size, so far 64 bytes is good for everyone */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000572 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000573}
574
Eric Biedermane9a271e32003-09-02 03:36:25 +0000575void pci_dev_enable_resources(struct device *dev)
576{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000577 const struct pci_operations *ops;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000578 uint16_t command;
Eric Biederman03acab62004-10-14 21:25:53 +0000579
580 /* Set the subsystem vendor and device id for mainboard devices */
581 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000582 if (dev->on_mainboard && ops && ops->set_subsystem) {
Eric Biederman03acab62004-10-14 21:25:53 +0000583 printk_debug("%s subsystem <- %02x/%02x\n",
584 dev_path(dev),
585 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
586 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
587 ops->set_subsystem(dev,
588 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
589 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
590 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000591 command = pci_read_config16(dev, PCI_COMMAND);
592 command |= dev->command;
Eric Biederman5cd81732004-03-11 15:01:31 +0000593 command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000594 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
595 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000596}
597
598void pci_bus_enable_resources(struct device *dev)
599{
600 uint16_t ctrl;
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000601 /* enable IO in command register if there is VGA card
602 * connected with (even it does not claim IO resource) */
603 if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
604 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000605 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
606 ctrl |= dev->link[0].bridge_ctrl;
Eric Biederman5cd81732004-03-11 15:01:31 +0000607 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000608 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
609 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
610
611 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000612
613 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000614}
615
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000616void pci_bus_reset(struct bus *bus)
617{
618 unsigned ctl;
619 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
620 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
621 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
622 mdelay(10);
623 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
624 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
625 delay(1);
626}
627
Eric Biedermandbec2d42004-10-21 10:44:08 +0000628void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000629{
630 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
631 ((device & 0xffff) << 16) | (vendor & 0xffff));
632}
633
Li-Ta Lo883b8792005-01-10 23:16:22 +0000634void pci_dev_init(struct device *dev)
635{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000636#if CONFIG_PCI_ROM_RUN == 1
Li-Ta Lo883b8792005-01-10 23:16:22 +0000637 struct rom_header *rom, *ram;
638
639 rom = pci_rom_probe(dev);
640 if (rom == NULL)
641 return;
642 ram = pci_rom_load(dev, rom);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000643 if (ram == NULL)
644 return;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000645
646 run_bios(dev, ram);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000647#endif
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000648}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000649
Li-Ta Loe5266692004-03-23 21:28:05 +0000650/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000651static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000652 .set_subsystem = pci_dev_set_subsystem,
653};
654
Eric Biederman8ca8d762003-04-22 19:02:15 +0000655struct device_operations default_pci_ops_dev = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000656 .read_resources = pci_dev_read_resources,
657 .set_resources = pci_dev_set_resources,
658 .enable_resources = pci_dev_enable_resources,
Li-Ta Lo883b8792005-01-10 23:16:22 +0000659 .init = pci_dev_init,
Li-Ta Loe5266692004-03-23 21:28:05 +0000660 .scan_bus = 0,
Eric Biederman03acab62004-10-14 21:25:53 +0000661 .enable = 0,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000662 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000663};
Li-Ta Loe5266692004-03-23 21:28:05 +0000664
665/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000666static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000667 .set_subsystem = 0,
668};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000669
Eric Biederman8ca8d762003-04-22 19:02:15 +0000670struct device_operations default_pci_ops_bus = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000671 .read_resources = pci_bus_read_resources,
672 .set_resources = pci_dev_set_resources,
673 .enable_resources = pci_bus_enable_resources,
Li-Ta Loe5266692004-03-23 21:28:05 +0000674 .init = 0,
675 .scan_bus = pci_scan_bridge,
Eric Biederman03acab62004-10-14 21:25:53 +0000676 .enable = 0,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000677 .reset_bus = pci_bus_reset,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000678 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000679};
Li-Ta Loe5266692004-03-23 21:28:05 +0000680
681/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000682 * @brief Detect the type of downstream bridge
683 *
684 * This function is a heuristic to detect which type
685 * of bus is downstream of a pci to pci bridge. This
686 * functions by looking for various capability blocks
687 * to figure out the type of downstream bridge. PCI-X
688 * PCI-E, and Hypertransport all seem to have appropriate
689 * capabilities.
690 *
691 * When only a PCI-Express capability is found the type
692 * is examined to see which type of bridge we have.
693 *
694 * @param dev
695 *
696 * @return appropriate bridge operations
697 */
698static struct device_operations *get_pci_bridge_ops(device_t dev)
699{
700 unsigned pos;
701
702#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
703 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
704 if (pos) {
705 printk_debug("%s subbordinate bus PCI-X\n", dev_path(dev));
706 return &default_pcix_ops_bus;
707 }
708#endif
709#if CONFIG_AGP_PLUGIN_SUPPORT == 1
710 /* How do I detect an PCI to AGP bridge? */
711#endif
712#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
713 pos = 0;
714 while((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
715 unsigned flags;
716 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
717 if ((flags >> 13) == 1) {
718 /* Host or Secondary Interface */
719 printk_debug("%s subbordinate bus Hypertransport\n",
720 dev_path(dev));
721 return &default_ht_ops_bus;
722 }
723 }
724#endif
725#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
726 pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
727 if (pos) {
728 unsigned flags;
729 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
730 switch((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
731 case PCI_EXP_TYPE_ROOT_PORT:
732 case PCI_EXP_TYPE_UPSTREAM:
733 case PCI_EXP_TYPE_DOWNSTREAM:
734 printk_debug("%s subbordinate bus PCI Express\n",
735 dev_path(dev));
736 return &default_pciexp_ops_bus;
737 case PCI_EXP_TYPE_PCI_BRIDGE:
738 printk_debug("%s subbordinate PCI\n",
739 dev_path(dev));
740 return &default_pci_ops_bus;
741 default:
742 break;
743 }
744 }
745#endif
746 return &default_pci_ops_bus;
747}
748
749/**
Li-Ta Loe5266692004-03-23 21:28:05 +0000750 * @brief Set up PCI device operation
751 *
752 *
753 * @param dev
754 *
755 * @see pci_drivers
756 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000757static void set_pci_ops(struct device *dev)
758{
759 struct pci_driver *driver;
760 if (dev->ops) {
761 return;
762 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000763
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +0000764 printk_debug("%s: seeking driver for %x:%x class %x\n",
765 __FUNCTION__, dev->vendor, dev->device, dev->class);
766 /* Look through the list of setup drivers and find one for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000767 * this pci device
768 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000769 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000770 if ((driver->vendor == dev->vendor) &&
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000771 (driver->device == dev->device))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000772 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000773 dev->ops = driver->ops;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000774 printk_spew("%s [%04x/%04x] %sops\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000775 dev_path(dev),
776 driver->vendor, driver->device,
777 (driver->ops->scan_bus?"bus ":""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000778 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000779 }
780 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000781
Eric Biederman8ca8d762003-04-22 19:02:15 +0000782 /* If I don't have a specific driver use the default operations */
783 switch(dev->hdr_type & 0x7f) { /* header type */
784 case PCI_HEADER_TYPE_NORMAL: /* standard header */
785 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
786 goto bad;
787 dev->ops = &default_pci_ops_dev;
788 break;
789 case PCI_HEADER_TYPE_BRIDGE:
790 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
791 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000792 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000793 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000794#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
795 case PCI_HEADER_TYPE_CARDBUS:
796 dev->ops = &default_cardbus_ops_bus;
797 break;
798#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000799 default:
800 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000801 if (dev->enabled) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000802 printk_err("%s [%04x/%04x/%06x] has unknown header "
Eric Biedermanb78c1972004-10-14 20:54:17 +0000803 "type %02x, ignoring.\n",
804 dev_path(dev),
805 dev->vendor, dev->device,
806 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000807 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000808 }
809 return;
810}
811
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000812
813
Eric Biederman8ca8d762003-04-22 19:02:15 +0000814/**
Eric Biederman03acab62004-10-14 21:25:53 +0000815 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000816 *
817 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000818 * device structure correspond to the devfn, if present. This function also
819 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000820 *
821 * @param list the device structure list
Eric Biederman8ca8d762003-04-22 19:02:15 +0000822 * @param devfn a device/function number
Li-Ta Loe5266692004-03-23 21:28:05 +0000823 *
Li-Ta Lo3a812852004-12-03 22:39:34 +0000824 * @return pointer to the device structure found or null of we have not
825 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000826 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000827static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000828{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000829 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000830 dev = 0;
831 for(; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000832 if ((*list)->path.type != DEVICE_PATH_PCI) {
Li-Ta Loe5266692004-03-23 21:28:05 +0000833 printk_err("child %s not a pci device\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000834 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000835 continue;
836 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000837 if ((*list)->path.u.pci.devfn == devfn) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000838 /* Unlink from the list */
839 dev = *list;
840 *list = (*list)->sibling;
841 dev->sibling = 0;
842 break;
843 }
844 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000845 /* Just like alloc_dev add the device to the list of device on the bus.
846 * When the list of devices was formed we removed all of the parents
847 * children, and now we are interleaving static and dynamic devices in
Li-Ta Lo3a812852004-12-03 22:39:34 +0000848 * order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000849 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000850 if (dev) {
851 device_t child;
852 /* Find the last child of our parent */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000853 for(child = dev->bus->children; child && child->sibling; ) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000854 child = child->sibling;
855 }
856 /* Place the device on the list of children of it's parent. */
857 if (child) {
858 child->sibling = dev;
859 } else {
860 dev->bus->children = dev;
861 }
862 }
863
Eric Biederman8ca8d762003-04-22 19:02:15 +0000864 return dev;
865}
866
Eric Biedermanb78c1972004-10-14 20:54:17 +0000867/**
868 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000869 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000870 * Determine the existence of a given PCI device.
871 *
872 * @param bus pointer to the bus structure
873 * @param devfn to look at
874 *
875 * @return The device structure for hte device (if found)
876 * or the NULL if no device is found.
877 */
878device_t pci_probe_dev(device_t dev, struct bus *bus, unsigned devfn)
879{
880 uint32_t id, class;
881 uint8_t hdr_type;
882
883 /* Detect if a device is present */
884 if (!dev) {
885 struct device dummy;
886 dummy.bus = bus;
887 dummy.path.type = DEVICE_PATH_PCI;
888 dummy.path.u.pci.devfn = devfn;
889 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
890 /* Have we found somthing?
891 * Some broken boards return 0 if a slot is empty.
892 */
893 if ( (id == 0xffffffff) || (id == 0x00000000) ||
894 (id == 0x0000ffff) || (id == 0xffff0000))
895 {
896 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
897 return NULL;
898 }
899 dev = alloc_dev(bus, &dummy.path);
900 }
901 else {
902 /* Enable/disable the device. Once we have
903 * found the device specific operations this
904 * operations we will disable the device with
905 * those as well.
906 *
907 * This is geared toward devices that have subfunctions
908 * that do not show up by default.
909 *
910 * If a device is a stuff option on the motherboard
911 * it may be absent and enable_dev must cope.
912 *
913 */
914 /* Run the magice enable sequence for the device */
915 if (dev->chip_ops && dev->chip_ops->enable_dev) {
916 dev->chip_ops->enable_dev(dev);
917 }
918 /* Now read the vendor and device id */
919 id = pci_read_config32(dev, PCI_VENDOR_ID);
920
921
922 /* If the device does not have a pci id disable it.
923 * Possibly this is because we have already disabled
924 * the device. But this also handles optional devices
925 * that may not always show up.
926 */
927 /* If the chain is fully enumerated quit */
928 if ( (id == 0xffffffff) || (id == 0x00000000) ||
929 (id == 0x0000ffff) || (id == 0xffff0000))
930 {
931 if (dev->enabled) {
932 printk_info("Disabling static device: %s\n",
933 dev_path(dev));
934 dev->enabled = 0;
935 }
936 return dev;
937 }
938 }
939 /* Read the rest of the pci configuration information */
940 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
941 class = pci_read_config32(dev, PCI_CLASS_REVISION);
942
943 /* Store the interesting information in the device structure */
944 dev->vendor = id & 0xffff;
945 dev->device = (id >> 16) & 0xffff;
946 dev->hdr_type = hdr_type;
947 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
948 dev->class = class >> 8;
949
950
951 /* Architectural/System devices always need to
952 * be bus masters.
953 */
954 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
955 dev->command |= PCI_COMMAND_MASTER;
956 }
957 /* Look at the vendor and device id, or at least the
958 * header type and class and figure out which set of
959 * configuration methods to use. Unless we already
960 * have some pci ops.
961 */
962 set_pci_ops(dev);
963
964 /* Now run the magic enable/disable sequence for the device */
965 if (dev->ops && dev->ops->enable) {
966 dev->ops->enable(dev);
967 }
968
969
970 /* Display the device and error if we don't have some pci operations
971 * for it.
972 */
973 printk_debug("%s [%04x/%04x] %s%s\n",
974 dev_path(dev),
975 dev->vendor, dev->device,
976 dev->enabled?"enabled": "disabled",
977 dev->ops?"" : " No operations"
978 );
979
980 return dev;
981}
982
983/**
984 * @brief Scan a PCI bus.
985 *
Li-Ta Loe5266692004-03-23 21:28:05 +0000986 * Determine the existence of devices and bridges on a PCI bus. If there are
987 * bridges on the bus, recursively scan the buses behind the bridges.
988 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000989 * This function is the default scan_bus() method for the root device
990 * 'dev_root'.
991 *
Eric Biedermane9a271e32003-09-02 03:36:25 +0000992 * @param bus pointer to the bus structure
993 * @param min_devfn minimum devfn to look at in the scan usually 0x00
994 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +0000995 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +0000996 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000997 * @return The maximum bus number found, after scanning all subordinate busses
998 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000999unsigned int pci_scan_bus(struct bus *bus,
1000 unsigned min_devfn, unsigned max_devfn,
1001 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001002{
1003 unsigned int devfn;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001004 device_t old_devices;
1005 device_t child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001006
1007 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
1008
1009 old_devices = bus->children;
1010 bus->children = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001011
1012 post_code(0x24);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001013 /* probe all devices/functions on this bus with some optimization for
Eric Biedermanb78c1972004-10-14 20:54:17 +00001014 * non-existence and single funcion devices
1015 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001016 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001017 device_t dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001018
Eric Biederman03acab62004-10-14 21:25:53 +00001019 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001020 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001021
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001022 /* See if a device is present and setup the device
1023 * structure.
Eric Biederman03acab62004-10-14 21:25:53 +00001024 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001025 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001026
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001027 /* if this is not a multi function device,
1028 * or the device is not present don't waste
1029 * time probing another function.
1030 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001031 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001032 if ((PCI_FUNC(devfn) == 0x00) &&
1033 (!dev || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80))))
1034 {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001035 devfn += 0x07;
1036 }
1037 }
1038 post_code(0x25);
1039
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001040 /* Die if any leftover Static devices are are found.
1041 * There's probably a problem in the Config.lb.
1042 */
1043 if(old_devices) {
1044 device_t left;
1045 for(left = old_devices; left; left = left->sibling) {
Ronald G. Minniche800b912006-01-17 21:12:03 +00001046 printk_err("%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001047 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +00001048 die("PCI: Left over static devices. Check your Config.lb\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001049 }
1050
Eric Biedermanb78c1972004-10-14 20:54:17 +00001051 /* For all children that implement scan_bus (i.e. bridges)
1052 * scan the bus behind that child.
1053 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001054 for(child = bus->children; child; child = child->sibling) {
1055 max = scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001056 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001057
Eric Biederman8ca8d762003-04-22 19:02:15 +00001058 /*
1059 * We've scanned the bus and so we know all about what's on
1060 * the other side of any bridges that may be on this bus plus
1061 * any devices.
1062 *
1063 * Return how far we've got finding sub-buses.
1064 */
1065 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
1066 post_code(0x55);
1067 return max;
1068}
1069
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001070
Li-Ta Loe5266692004-03-23 21:28:05 +00001071/**
1072 * @brief Scan a PCI bridge and the buses behind the bridge.
1073 *
1074 * Determine the existence of buses behind the bridge. Set up the bridge
1075 * according to the result of the scan.
1076 *
1077 * This function is the default scan_bus() method for PCI bridge devices.
1078 *
1079 * @param dev pointer to the bridge device
1080 * @param max the highest bus number assgined up to now
1081 *
Eric Biederman8ca8d762003-04-22 19:02:15 +00001082 * @return The maximum bus number found, after scanning all subordinate busses
1083 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001084unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
1085 unsigned int (*do_scan_bus)(struct bus *bus,
1086 unsigned min_devfn, unsigned max_devfn, unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001087{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001088 struct bus *bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001089 uint32_t buses;
1090 uint16_t cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001091
Li-Ta Lo3a812852004-12-03 22:39:34 +00001092 printk_spew("%s for %s\n", __func__, dev_path(dev));
1093
Eric Biedermane9a271e32003-09-02 03:36:25 +00001094 bus = &dev->link[0];
Eric Biedermana9e632c2004-11-18 22:38:08 +00001095 bus->dev = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001096 dev->links = 1;
1097
Eric Biederman8ca8d762003-04-22 19:02:15 +00001098 /* Set up the primary, secondary and subordinate bus numbers. We have
1099 * no idea how many buses are behind this bridge yet, so we set the
Eric Biedermanb78c1972004-10-14 20:54:17 +00001100 * subordinate bus number to 0xff for the moment.
1101 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001102 bus->secondary = ++max;
1103 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001104
Eric Biederman8ca8d762003-04-22 19:02:15 +00001105 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001106 cr = pci_read_config16(dev, PCI_COMMAND);
1107 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1108 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001109
Eric Biedermanb78c1972004-10-14 20:54:17 +00001110 /*
1111 * Read the existing primary/secondary/subordinate bus
1112 * number configuration.
1113 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001114 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001115
1116 /* Configure the bus numbers for this bridge: the configuration
1117 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001118 * correctly configured.
1119 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001120 buses &= 0xff000000;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001121 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001122 ((unsigned int) (bus->secondary) << 8) |
1123 ((unsigned int) (bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001124 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001125
Eric Biedermanb78c1972004-10-14 20:54:17 +00001126 /* Now we can scan all subordinate buses
1127 * i.e. the bus behind the bridge.
1128 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001129 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001130
Eric Biederman8ca8d762003-04-22 19:02:15 +00001131 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001132 * bus number to its real value.
1133 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001134 bus->subordinate = max;
1135 buses = (buses & 0xff00ffff) |
1136 ((unsigned int) (bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001137 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1138 pci_write_config16(dev, PCI_COMMAND, cr);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001139
Eric Biedermanb78c1972004-10-14 20:54:17 +00001140 printk_spew("%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001141 return max;
1142}
Li-Ta Loe5266692004-03-23 21:28:05 +00001143
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001144/**
1145 * @brief Scan a PCI bridge and the buses behind the bridge.
1146 *
1147 * Determine the existence of buses behind the bridge. Set up the bridge
1148 * according to the result of the scan.
1149 *
1150 * This function is the default scan_bus() method for PCI bridge devices.
1151 *
1152 * @param dev pointer to the bridge device
1153 * @param max the highest bus number assgined up to now
1154 *
1155 * @return The maximum bus number found, after scanning all subordinate busses
1156 */
1157unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1158{
1159 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1160}
1161
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001162/*
1163 Tell the EISA int controller this int must be level triggered
1164 THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
1165*/
Ronald G. Minnich88fb1a62006-06-22 04:37:27 +00001166void pci_level_irq(unsigned char intNum)
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001167{
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001168 unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001169
Eric Biedermanb78c1972004-10-14 20:54:17 +00001170 printk_spew("%s: current ints are 0x%x\n", __func__, intBits);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001171 intBits |= (1 << intNum);
1172
Eric Biedermanb78c1972004-10-14 20:54:17 +00001173 printk_spew("%s: try to set ints 0x%x\n", __func__, intBits);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001174
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001175 // Write new values
1176 outb((unsigned char) intBits, 0x4d0);
1177 outb((unsigned char) (intBits >> 8), 0x4d1);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001178
Ronald G. Minnichb56ef072003-10-15 20:05:11 +00001179 /* this seems like an error but is not ... */
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001180#if 1
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001181 if (inb(0x4d0) != (intBits & 0xff)) {
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001182 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001183 __func__, intBits &0xff, inb(0x4d0));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001184 }
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001185 if (inb(0x4d1) != ((intBits >> 8) & 0xff)) {
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001186 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001187 __func__, (intBits>>8) &0xff, inb(0x4d1));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001188 }
Ronald G. Minnichb56ef072003-10-15 20:05:11 +00001189#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001190}
1191
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001192/*
1193 This function assigns IRQs for all functions contained within
1194 the indicated device address. If the device does not exist or does
1195 not require interrupts then this function has no effect.
1196
1197 This function should be called for each PCI slot in your system.
1198
1199 pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
1200 this slot.
1201 The particular irq #s that are passed in depend on the routing inside
1202 your southbridge and on your motherboard.
1203
1204 -kevinh@ispiri.com
1205*/
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001206void pci_assign_irqs(unsigned bus, unsigned slot,
1207 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001208{
1209 unsigned functNum;
1210 device_t pdev;
1211 unsigned char line;
1212 unsigned char irq;
1213 unsigned char readback;
1214
1215 /* Each slot may contain up to eight functions */
1216 for (functNum = 0; functNum < 8; functNum++) {
1217 pdev = dev_find_slot(bus, (slot << 3) + functNum);
1218
1219 if (pdev) {
1220 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1221
1222 // PCI spec says all other values are reserved
1223 if ((line >= 1) && (line <= 4)) {
1224 irq = pIntAtoD[line - 1];
1225
1226 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
1227 irq, bus, slot, functNum);
1228
1229 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
1230 pIntAtoD[line - 1]);
1231
1232 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1233 printk_debug(" Readback = %d\n", readback);
1234
1235 // Change to level triggered
1236 pci_level_irq(pIntAtoD[line - 1]);
1237 }
1238 }
1239 }
1240}