wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | a47a12b | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 2 | * arch/powerpc/kernel/pci_auto.c |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * |
| 4 | * PCI autoconfiguration library |
| 5 | * |
| 6 | * Author: Matt Porter <mporter@mvista.com> |
| 7 | * |
| 8 | * Copyright 2000 MontaVista Software Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 18 | #include <pci.h> |
| 19 | |
| 20 | #undef DEBUG |
| 21 | #ifdef DEBUG |
| 22 | #define DEBUGF(x...) printf(x) |
| 23 | #else |
| 24 | #define DEBUGF(x...) |
| 25 | #endif /* DEBUG */ |
| 26 | |
| 27 | #define PCIAUTO_IDE_MODE_MASK 0x05 |
| 28 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 29 | /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */ |
| 30 | #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE |
| 31 | #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8 |
Gary Jennejohn | 81b73de | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 34 | /* |
| 35 | * |
| 36 | */ |
| 37 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 38 | void pciauto_region_init(struct pci_region *res) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | { |
Sergei Shtylyov | b7598a4 | 2007-04-23 15:30:39 +0200 | [diff] [blame] | 40 | /* |
| 41 | * Avoid allocating PCI resources from address 0 -- this is illegal |
| 42 | * according to PCI 2.1 and moreover, this is known to cause Linux IDE |
| 43 | * drivers to fail. Use a reasonable starting value of 0x1000 instead. |
| 44 | */ |
| 45 | res->bus_lower = res->bus_start ? res->bus_start : 0x1000; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 48 | void pciauto_region_align(struct pci_region *res, pci_size_t size) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | { |
| 50 | res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 51 | } |
| 52 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 53 | int pciauto_region_allocate(struct pci_region *res, pci_size_t size, |
| 54 | pci_addr_t *bar) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | { |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 56 | pci_addr_t addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 57 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 58 | if (!res) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 59 | DEBUGF("No resource"); |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | addr = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 64 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 65 | if (addr - res->bus_start + size > res->size) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 66 | DEBUGF("No room in resource"); |
| 67 | goto error; |
| 68 | } |
| 69 | |
| 70 | res->bus_lower = addr + size; |
| 71 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 72 | DEBUGF("address=0x%llx bus_lower=0x%llx", (u64)addr, (u64)res->bus_lower); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | |
| 74 | *bar = addr; |
| 75 | return 0; |
| 76 | |
| 77 | error: |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 78 | *bar = (pci_addr_t)-1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * |
| 84 | */ |
| 85 | |
| 86 | void pciauto_setup_device(struct pci_controller *hose, |
| 87 | pci_dev_t dev, int bars_num, |
| 88 | struct pci_region *mem, |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 89 | struct pci_region *prefetch, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 90 | struct pci_region *io) |
| 91 | { |
Kumar Gala | cf5787f | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 92 | u32 bar_response; |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 93 | pci_size_t bar_size; |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 94 | u16 cmdstat = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 95 | int bar, bar_nr = 0; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 96 | #ifndef CONFIG_PCI_ENUM_ONLY |
| 97 | pci_addr_t bar_value; |
| 98 | struct pci_region *bar_res; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | int found_mem64 = 0; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 100 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 101 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 102 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER; |
| 104 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 105 | for (bar = PCI_BASE_ADDRESS_0; |
| 106 | bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 107 | /* Tickle the BAR and get the response */ |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 108 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 109 | pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 110 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | pci_hose_read_config_dword(hose, dev, bar, &bar_response); |
| 112 | |
| 113 | /* If BAR is not implemented go to the next BAR */ |
| 114 | if (!bar_response) |
| 115 | continue; |
| 116 | |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 117 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 118 | found_mem64 = 0; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 119 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | |
| 121 | /* Check the BAR type and set our address mask */ |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 122 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
Jin Zhengxiong-R64188 | bd22c2b | 2006-06-27 18:12:02 +0800 | [diff] [blame] | 123 | bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK)) |
| 124 | & 0xffff) + 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 125 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | bar_res = io; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 127 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 129 | DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ", bar_nr, (u64)bar_size); |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 130 | } else { |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 131 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 132 | PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 133 | u32 bar_response_upper; |
| 134 | u64 bar64; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 135 | |
| 136 | #ifndef CONFIG_PCI_ENUM_ONLY |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 137 | pci_hose_write_config_dword(hose, dev, bar + 4, |
| 138 | 0xffffffff); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 139 | #endif |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 140 | pci_hose_read_config_dword(hose, dev, bar + 4, |
| 141 | &bar_response_upper); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 142 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 143 | bar64 = ((u64)bar_response_upper << 32) | bar_response; |
| 144 | |
| 145 | bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 146 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 147 | found_mem64 = 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 148 | #endif |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 149 | } else { |
| 150 | bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1); |
| 151 | } |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 152 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 153 | if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH)) |
| 154 | bar_res = prefetch; |
| 155 | else |
| 156 | bar_res = mem; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 157 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 159 | DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%llx, ", bar_nr, (u64)bar_size); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 162 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 163 | if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 164 | /* Write it out and update our limit */ |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 165 | pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 166 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 167 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 168 | bar += 4; |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 169 | #ifdef CONFIG_SYS_PCI_64BIT |
| 170 | pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32)); |
| 171 | #else |
| 172 | /* |
| 173 | * If we are a 64-bit decoder then increment to the |
| 174 | * upper 32 bits of the bar and force it to locate |
| 175 | * in the lower 4GB of memory. |
| 176 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 177 | pci_hose_write_config_dword(hose, dev, bar, 0x00000000); |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 178 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 179 | } |
| 180 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 181 | } |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 182 | #endif |
| 183 | cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ? |
| 184 | PCI_COMMAND_IO : PCI_COMMAND_MEMORY; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 185 | |
| 186 | DEBUGF("\n"); |
| 187 | |
| 188 | bar_nr++; |
| 189 | } |
| 190 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 191 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat); |
Gary Jennejohn | 81b73de | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 192 | pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 193 | CONFIG_SYS_PCI_CACHE_LINE_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 194 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
| 195 | } |
| 196 | |
Ed Swarthout | ba5feb1 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 197 | void pciauto_prescan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 198 | pci_dev_t dev, int sub_bus) |
| 199 | { |
| 200 | struct pci_region *pci_mem = hose->pci_mem; |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 201 | struct pci_region *pci_prefetch = hose->pci_prefetch; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 202 | struct pci_region *pci_io = hose->pci_io; |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 203 | u16 cmdstat; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 205 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | |
| 207 | /* Configure bus number registers */ |
Ed Swarthout | e8b85f3 | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 208 | pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, |
| 209 | PCI_BUS(dev) - hose->first_busno); |
| 210 | pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, |
| 211 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 212 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff); |
| 213 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 214 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 215 | /* Round memory allocator to 1MB boundary */ |
| 216 | pciauto_region_align(pci_mem, 0x100000); |
| 217 | |
| 218 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 219 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE, |
| 220 | (pci_mem->bus_lower & 0xfff00000) >> 16); |
| 221 | |
| 222 | cmdstat |= PCI_COMMAND_MEMORY; |
| 223 | } |
| 224 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 225 | if (pci_prefetch) { |
| 226 | /* Round memory allocator to 1MB boundary */ |
| 227 | pciauto_region_align(pci_prefetch, 0x100000); |
| 228 | |
| 229 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 230 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, |
| 231 | (pci_prefetch->bus_lower & 0xfff00000) >> 16); |
| 232 | |
| 233 | cmdstat |= PCI_COMMAND_MEMORY; |
| 234 | } else { |
| 235 | /* We don't support prefetchable memory for now, so disable */ |
| 236 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000); |
Matthew McClintock | a4e1155 | 2006-06-28 10:44:23 -0500 | [diff] [blame] | 237 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0); |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 238 | } |
| 239 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 240 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | /* Round I/O allocator to 4KB boundary */ |
| 242 | pciauto_region_align(pci_io, 0x1000); |
| 243 | |
| 244 | pci_hose_write_config_byte(hose, dev, PCI_IO_BASE, |
| 245 | (pci_io->bus_lower & 0x0000f000) >> 8); |
| 246 | pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16, |
| 247 | (pci_io->bus_lower & 0xffff0000) >> 16); |
| 248 | |
| 249 | cmdstat |= PCI_COMMAND_IO; |
| 250 | } |
| 251 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 252 | /* Enable memory and I/O accesses, enable bus master */ |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 253 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, |
| 254 | cmdstat | PCI_COMMAND_MASTER); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Ed Swarthout | ba5feb1 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 257 | void pciauto_postscan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 258 | pci_dev_t dev, int sub_bus) |
| 259 | { |
| 260 | struct pci_region *pci_mem = hose->pci_mem; |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 261 | struct pci_region *pci_prefetch = hose->pci_prefetch; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 262 | struct pci_region *pci_io = hose->pci_io; |
| 263 | |
| 264 | /* Configure bus number registers */ |
Ed Swarthout | e8b85f3 | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 265 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, |
| 266 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 267 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 268 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 269 | /* Round memory allocator to 1MB boundary */ |
| 270 | pciauto_region_align(pci_mem, 0x100000); |
| 271 | |
| 272 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 273 | (pci_mem->bus_lower - 1) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 276 | if (pci_prefetch) { |
| 277 | /* Round memory allocator to 1MB boundary */ |
| 278 | pciauto_region_align(pci_prefetch, 0x100000); |
| 279 | |
| 280 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 281 | (pci_prefetch->bus_lower - 1) >> 16); |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 282 | } |
| 283 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 284 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 285 | /* Round I/O allocator to 4KB boundary */ |
| 286 | pciauto_region_align(pci_io, 0x1000); |
| 287 | |
| 288 | pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 289 | ((pci_io->bus_lower - 1) & 0x0000f000) >> 8); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 290 | pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 291 | ((pci_io->bus_lower - 1) & 0xffff0000) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * |
| 297 | */ |
| 298 | |
| 299 | void pciauto_config_init(struct pci_controller *hose) |
| 300 | { |
| 301 | int i; |
| 302 | |
| 303 | hose->pci_io = hose->pci_mem = NULL; |
| 304 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 305 | for (i = 0; i < hose->region_count; i++) { |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 306 | switch(hose->regions[i].flags) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 307 | case PCI_REGION_IO: |
| 308 | if (!hose->pci_io || |
| 309 | hose->pci_io->size < hose->regions[i].size) |
| 310 | hose->pci_io = hose->regions + i; |
| 311 | break; |
| 312 | case PCI_REGION_MEM: |
| 313 | if (!hose->pci_mem || |
| 314 | hose->pci_mem->size < hose->regions[i].size) |
| 315 | hose->pci_mem = hose->regions + i; |
| 316 | break; |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 317 | case (PCI_REGION_MEM | PCI_REGION_PREFETCH): |
| 318 | if (!hose->pci_prefetch || |
| 319 | hose->pci_prefetch->size < hose->regions[i].size) |
| 320 | hose->pci_prefetch = hose->regions + i; |
| 321 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 326 | if (hose->pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 327 | pciauto_region_init(hose->pci_mem); |
| 328 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 329 | DEBUGF("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n" |
| 330 | "\t\tPhysical Memory [%llx-%llxx]\n", |
| 331 | (u64)hose->pci_mem->bus_start, |
| 332 | (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1), |
| 333 | (u64)hose->pci_mem->phys_start, |
| 334 | (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 337 | if (hose->pci_prefetch) { |
| 338 | pciauto_region_init(hose->pci_prefetch); |
| 339 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 340 | DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n" |
| 341 | "\t\tPhysical Memory [%llx-%llx]\n", |
| 342 | (u64)hose->pci_prefetch->bus_start, |
| 343 | (u64)(hose->pci_prefetch->bus_start + |
| 344 | hose->pci_prefetch->size - 1), |
| 345 | (u64)hose->pci_prefetch->phys_start, |
| 346 | (u64)(hose->pci_prefetch->phys_start + |
| 347 | hose->pci_prefetch->size - 1)); |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 348 | } |
| 349 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 350 | if (hose->pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 351 | pciauto_region_init(hose->pci_io); |
| 352 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 353 | DEBUGF("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n" |
| 354 | "\t\tPhysical Memory: [%llx-%llx]\n", |
| 355 | (u64)hose->pci_io->bus_start, |
| 356 | (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1), |
| 357 | (u64)hose->pci_io->phys_start, |
| 358 | (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1)); |
Ed Swarthout | ba5feb1 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 359 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 363 | /* |
| 364 | * HJF: Changed this to return int. I think this is required |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 365 | * to get the correct result when scanning bridges |
| 366 | */ |
| 367 | int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 368 | { |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 369 | unsigned int sub_bus = PCI_BUS(dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 370 | unsigned short class; |
| 371 | unsigned char prg_iface; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 372 | int n; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 373 | |
| 374 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 375 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 376 | switch (class) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 377 | case PCI_CLASS_BRIDGE_PCI: |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 378 | hose->current_busno++; |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 379 | pciauto_setup_device(hose, dev, 2, hose->pci_mem, |
| 380 | hose->pci_prefetch, hose->pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 381 | |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 382 | DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev)); |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 383 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 384 | /* Passing in current_busno allows for sibling P2P bridges */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 385 | pciauto_prescan_setup_bridge(hose, dev, hose->current_busno); |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 386 | /* |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 387 | * need to figure out if this is a subordinate bridge on the bus |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 388 | * to be able to properly set the pri/sec/sub bridge registers. |
| 389 | */ |
| 390 | n = pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 391 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 392 | /* figure out the deepest we've gone for this leg */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 393 | sub_bus = max(n, sub_bus); |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 394 | pciauto_postscan_setup_bridge(hose, dev, sub_bus); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 395 | |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 396 | sub_bus = hose->current_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 397 | break; |
| 398 | |
| 399 | case PCI_CLASS_STORAGE_IDE: |
| 400 | pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface); |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 401 | if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) { |
| 402 | DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n"); |
| 403 | return sub_bus; |
| 404 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 405 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 406 | pciauto_setup_device(hose, dev, 6, hose->pci_mem, |
| 407 | hose->pci_prefetch, hose->pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 408 | break; |
| 409 | |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 410 | case PCI_CLASS_BRIDGE_CARDBUS: |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 411 | /* |
| 412 | * just do a minimal setup of the bridge, |
| 413 | * let the OS take care of the rest |
| 414 | */ |
| 415 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, |
| 416 | hose->pci_prefetch, hose->pci_io); |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 417 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 418 | DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n", |
| 419 | PCI_DEV(dev)); |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 420 | |
| 421 | hose->current_busno++; |
| 422 | break; |
| 423 | |
TsiChung Liew | f33fca2 | 2008-03-30 01:19:06 -0500 | [diff] [blame] | 424 | #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE) |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 425 | case PCI_CLASS_BRIDGE_OTHER: |
| 426 | DEBUGF("PCI Autoconfig: Skipping bridge device %d\n", |
| 427 | PCI_DEV(dev)); |
| 428 | break; |
| 429 | #endif |
Reinhard Arlt | c2e49f7 | 2009-07-25 06:19:12 +0200 | [diff] [blame] | 430 | #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349) |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 431 | case PCI_CLASS_BRIDGE_OTHER: |
| 432 | /* |
| 433 | * The host/PCI bridge 1 seems broken in 8349 - it presents |
| 434 | * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_ |
| 435 | * device claiming resources io/mem/irq.. we only allow for |
| 436 | * the PIMMR window to be allocated (BAR0 - 1MB size) |
| 437 | */ |
| 438 | DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n"); |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 439 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, |
| 440 | hose->pci_prefetch, hose->pci_io); |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 441 | break; |
| 442 | #endif |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 443 | |
| 444 | case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */ |
| 445 | DEBUGF("PCI AutoConfig: Found PowerPC device\n"); |
| 446 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 447 | default: |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 448 | pciauto_setup_device(hose, dev, 6, hose->pci_mem, |
| 449 | hose->pci_prefetch, hose->pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 450 | break; |
| 451 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 452 | |
| 453 | return sub_bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 454 | } |