Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 2 | /* |
Simon Glass | 2b81e8a | 2015-11-29 13:17:46 -0700 | [diff] [blame] | 3 | * PCI autoconfiguration library (legacy version, do not change) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 4 | * |
| 5 | * Author: Matt Porter <mporter@mvista.com> |
| 6 | * |
| 7 | * Copyright 2000 MontaVista Software Inc. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 4a2708a | 2015-01-14 21:37:04 -0700 | [diff] [blame] | 11 | #include <errno.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 12 | #include <pci.h> |
| 13 | |
Simon Glass | 2b81e8a | 2015-11-29 13:17:46 -0700 | [diff] [blame] | 14 | /* |
| 15 | * Do not change this file. Instead, convert your board to use CONFIG_DM_PCI |
| 16 | * and change pci_auto.c. |
| 17 | */ |
| 18 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 19 | /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */ |
| 20 | #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE |
| 21 | #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8 |
Gary Jennejohn | 81b73de | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 22 | #endif |
| 23 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | /* |
| 25 | * |
| 26 | */ |
| 27 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 28 | void pciauto_setup_device(struct pci_controller *hose, |
| 29 | pci_dev_t dev, int bars_num, |
| 30 | struct pci_region *mem, |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 31 | struct pci_region *prefetch, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | struct pci_region *io) |
| 33 | { |
Kumar Gala | cf5787f | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 34 | u32 bar_response; |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 35 | pci_size_t bar_size; |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 36 | u16 cmdstat = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | int bar, bar_nr = 0; |
Simon Glass | 53292ad | 2015-07-31 09:31:34 -0600 | [diff] [blame] | 38 | #ifndef CONFIG_PCI_ENUM_ONLY |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 39 | u8 header_type; |
| 40 | int rom_addr; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 41 | pci_addr_t bar_value; |
| 42 | struct pci_region *bar_res; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 43 | int found_mem64 = 0; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 44 | #endif |
Bin Meng | cdf9f08 | 2015-10-01 00:35:59 -0700 | [diff] [blame] | 45 | u16 class; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 47 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER; |
| 49 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 50 | for (bar = PCI_BASE_ADDRESS_0; |
| 51 | bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | /* Tickle the BAR and get the response */ |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 53 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 55 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | pci_hose_read_config_dword(hose, dev, bar, &bar_response); |
| 57 | |
| 58 | /* If BAR is not implemented go to the next BAR */ |
| 59 | if (!bar_response) |
| 60 | continue; |
| 61 | |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 62 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | found_mem64 = 0; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 64 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | |
| 66 | /* Check the BAR type and set our address mask */ |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 67 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
Jin Zhengxiong-R64188 | bd22c2b | 2006-06-27 18:12:02 +0800 | [diff] [blame] | 68 | bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK)) |
| 69 | & 0xffff) + 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 70 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 71 | bar_res = io; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 72 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 74 | debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ", |
| 75 | bar_nr, (unsigned long long)bar_size); |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 76 | } else { |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 77 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 78 | PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 79 | u32 bar_response_upper; |
| 80 | u64 bar64; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 81 | |
| 82 | #ifndef CONFIG_PCI_ENUM_ONLY |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 83 | pci_hose_write_config_dword(hose, dev, bar + 4, |
| 84 | 0xffffffff); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 85 | #endif |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 86 | pci_hose_read_config_dword(hose, dev, bar + 4, |
| 87 | &bar_response_upper); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 88 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 89 | bar64 = ((u64)bar_response_upper << 32) | bar_response; |
| 90 | |
| 91 | bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 92 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 93 | found_mem64 = 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 94 | #endif |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 95 | } else { |
| 96 | bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1); |
| 97 | } |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 98 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 99 | if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH)) |
| 100 | bar_res = prefetch; |
| 101 | else |
| 102 | bar_res = mem; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | |
Simon Glass | 4bad2e7 | 2015-07-27 15:47:18 -0600 | [diff] [blame] | 104 | debug("PCI Autoconfig: BAR %d, %s, size=0x%llx, ", |
| 105 | bar_nr, bar_res == prefetch ? "Prf" : "Mem", |
| 106 | (unsigned long long)bar_size); |
Phil Sutter | 1113146 | 2015-12-25 14:41:17 +0100 | [diff] [blame] | 107 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 110 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 111 | if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 112 | /* Write it out and update our limit */ |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 113 | pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 115 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | bar += 4; |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 117 | #ifdef CONFIG_SYS_PCI_64BIT |
| 118 | pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32)); |
| 119 | #else |
| 120 | /* |
| 121 | * If we are a 64-bit decoder then increment to the |
| 122 | * upper 32 bits of the bar and force it to locate |
| 123 | * in the lower 4GB of memory. |
| 124 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 125 | pci_hose_write_config_dword(hose, dev, bar, 0x00000000); |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 126 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | } |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 130 | #endif |
| 131 | cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ? |
| 132 | PCI_COMMAND_IO : PCI_COMMAND_MEMORY; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 133 | |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 134 | debug("\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 135 | |
| 136 | bar_nr++; |
| 137 | } |
| 138 | |
Simon Glass | 53292ad | 2015-07-31 09:31:34 -0600 | [diff] [blame] | 139 | #ifndef CONFIG_PCI_ENUM_ONLY |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 140 | /* Configure the expansion ROM address */ |
| 141 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type); |
Bin Meng | 7445435 | 2015-10-07 02:13:18 -0700 | [diff] [blame] | 142 | header_type &= 0x7f; |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 143 | if (header_type != PCI_HEADER_TYPE_CARDBUS) { |
| 144 | rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ? |
| 145 | PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1; |
| 146 | pci_hose_write_config_dword(hose, dev, rom_addr, 0xfffffffe); |
| 147 | pci_hose_read_config_dword(hose, dev, rom_addr, &bar_response); |
| 148 | if (bar_response) { |
| 149 | bar_size = -(bar_response & ~1); |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 150 | debug("PCI Autoconfig: ROM, size=%#x, ", |
| 151 | (unsigned int)bar_size); |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 152 | if (pciauto_region_allocate(mem, bar_size, |
| 153 | &bar_value) == 0) { |
| 154 | pci_hose_write_config_dword(hose, dev, rom_addr, |
| 155 | bar_value); |
| 156 | } |
| 157 | cmdstat |= PCI_COMMAND_MEMORY; |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 158 | debug("\n"); |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 159 | } |
| 160 | } |
Simon Glass | 53292ad | 2015-07-31 09:31:34 -0600 | [diff] [blame] | 161 | #endif |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 162 | |
Bin Meng | cdf9f08 | 2015-10-01 00:35:59 -0700 | [diff] [blame] | 163 | /* PCI_COMMAND_IO must be set for VGA device */ |
| 164 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 165 | if (class == PCI_CLASS_DISPLAY_VGA) |
| 166 | cmdstat |= PCI_COMMAND_IO; |
| 167 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 168 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat); |
Gary Jennejohn | 81b73de | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 169 | 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] | 170 | CONFIG_SYS_PCI_CACHE_LINE_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 171 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
| 172 | } |
| 173 | |
Ed Swarthout | ba5feb1 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 174 | void pciauto_prescan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 175 | pci_dev_t dev, int sub_bus) |
| 176 | { |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 177 | struct pci_region *pci_mem; |
| 178 | struct pci_region *pci_prefetch; |
| 179 | struct pci_region *pci_io; |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 180 | u16 cmdstat, prefechable_64; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 181 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 182 | pci_mem = hose->pci_mem; |
| 183 | pci_prefetch = hose->pci_prefetch; |
| 184 | pci_io = hose->pci_io; |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 185 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 186 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 187 | pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE, |
| 188 | &prefechable_64); |
| 189 | prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 190 | |
| 191 | /* Configure bus number registers */ |
Ed Swarthout | e8b85f3 | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 192 | pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, |
| 193 | PCI_BUS(dev) - hose->first_busno); |
| 194 | pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, |
| 195 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 196 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff); |
| 197 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 198 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 199 | /* Round memory allocator to 1MB boundary */ |
| 200 | pciauto_region_align(pci_mem, 0x100000); |
| 201 | |
| 202 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 203 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE, |
| 204 | (pci_mem->bus_lower & 0xfff00000) >> 16); |
| 205 | |
| 206 | cmdstat |= PCI_COMMAND_MEMORY; |
| 207 | } |
| 208 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 209 | if (pci_prefetch) { |
| 210 | /* Round memory allocator to 1MB boundary */ |
| 211 | pciauto_region_align(pci_prefetch, 0x100000); |
| 212 | |
| 213 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 214 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, |
| 215 | (pci_prefetch->bus_lower & 0xfff00000) >> 16); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 216 | if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) |
| 217 | #ifdef CONFIG_SYS_PCI_64BIT |
| 218 | pci_hose_write_config_dword(hose, dev, |
| 219 | PCI_PREF_BASE_UPPER32, |
| 220 | pci_prefetch->bus_lower >> 32); |
| 221 | #else |
| 222 | pci_hose_write_config_dword(hose, dev, |
| 223 | PCI_PREF_BASE_UPPER32, |
| 224 | 0x0); |
| 225 | #endif |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 226 | |
| 227 | cmdstat |= PCI_COMMAND_MEMORY; |
| 228 | } else { |
| 229 | /* We don't support prefetchable memory for now, so disable */ |
| 230 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000); |
Matthew McClintock | a4e1155 | 2006-06-28 10:44:23 -0500 | [diff] [blame] | 231 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 232 | if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) { |
| 233 | pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0); |
| 234 | pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0); |
| 235 | } |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 236 | } |
| 237 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 238 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 239 | /* Round I/O allocator to 4KB boundary */ |
| 240 | pciauto_region_align(pci_io, 0x1000); |
| 241 | |
| 242 | pci_hose_write_config_byte(hose, dev, PCI_IO_BASE, |
| 243 | (pci_io->bus_lower & 0x0000f000) >> 8); |
| 244 | pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16, |
| 245 | (pci_io->bus_lower & 0xffff0000) >> 16); |
| 246 | |
| 247 | cmdstat |= PCI_COMMAND_IO; |
| 248 | } |
| 249 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 250 | /* Enable memory and I/O accesses, enable bus master */ |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 251 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, |
| 252 | cmdstat | PCI_COMMAND_MASTER); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Ed Swarthout | ba5feb1 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 255 | void pciauto_postscan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 256 | pci_dev_t dev, int sub_bus) |
| 257 | { |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 258 | struct pci_region *pci_mem; |
| 259 | struct pci_region *pci_prefetch; |
| 260 | struct pci_region *pci_io; |
| 261 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 262 | pci_mem = hose->pci_mem; |
| 263 | pci_prefetch = hose->pci_prefetch; |
| 264 | pci_io = hose->pci_io; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 265 | |
| 266 | /* Configure bus number registers */ |
Ed Swarthout | e8b85f3 | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 267 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, |
| 268 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 269 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 270 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 271 | /* Round memory allocator to 1MB boundary */ |
| 272 | pciauto_region_align(pci_mem, 0x100000); |
| 273 | |
| 274 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 275 | (pci_mem->bus_lower - 1) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 278 | if (pci_prefetch) { |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 279 | u16 prefechable_64; |
| 280 | |
| 281 | pci_hose_read_config_word(hose, dev, |
| 282 | PCI_PREF_MEMORY_LIMIT, |
| 283 | &prefechable_64); |
| 284 | prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK; |
| 285 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 286 | /* Round memory allocator to 1MB boundary */ |
| 287 | pciauto_region_align(pci_prefetch, 0x100000); |
| 288 | |
| 289 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 290 | (pci_prefetch->bus_lower - 1) >> 16); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 291 | if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) |
| 292 | #ifdef CONFIG_SYS_PCI_64BIT |
| 293 | pci_hose_write_config_dword(hose, dev, |
| 294 | PCI_PREF_LIMIT_UPPER32, |
| 295 | (pci_prefetch->bus_lower - 1) >> 32); |
| 296 | #else |
| 297 | pci_hose_write_config_dword(hose, dev, |
| 298 | PCI_PREF_LIMIT_UPPER32, |
| 299 | 0x0); |
| 300 | #endif |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 301 | } |
| 302 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 303 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 304 | /* Round I/O allocator to 4KB boundary */ |
| 305 | pciauto_region_align(pci_io, 0x1000); |
| 306 | |
| 307 | pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 308 | ((pci_io->bus_lower - 1) & 0x0000f000) >> 8); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 309 | pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 310 | ((pci_io->bus_lower - 1) & 0xffff0000) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 314 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 315 | /* |
| 316 | * HJF: Changed this to return int. I think this is required |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 317 | * to get the correct result when scanning bridges |
| 318 | */ |
| 319 | int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 320 | { |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 321 | struct pci_region *pci_mem; |
| 322 | struct pci_region *pci_prefetch; |
| 323 | struct pci_region *pci_io; |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 324 | unsigned int sub_bus = PCI_BUS(dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 325 | unsigned short class; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 326 | int n; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 327 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 328 | pci_mem = hose->pci_mem; |
| 329 | pci_prefetch = hose->pci_prefetch; |
| 330 | pci_io = hose->pci_io; |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 331 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 332 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 333 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 334 | switch (class) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 335 | case PCI_CLASS_BRIDGE_PCI: |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 336 | debug("PCI Autoconfig: Found P2P bridge, device %d\n", |
| 337 | PCI_DEV(dev)); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 338 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 339 | pciauto_setup_device(hose, dev, 2, pci_mem, |
| 340 | pci_prefetch, pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 341 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 342 | /* Passing in current_busno allows for sibling P2P bridges */ |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 343 | hose->current_busno++; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 344 | pciauto_prescan_setup_bridge(hose, dev, hose->current_busno); |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 345 | /* |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 346 | * need to figure out if this is a subordinate bridge on the bus |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 347 | * to be able to properly set the pri/sec/sub bridge registers. |
| 348 | */ |
| 349 | n = pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 350 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 351 | /* figure out the deepest we've gone for this leg */ |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 352 | sub_bus = max((unsigned int)n, sub_bus); |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 353 | pciauto_postscan_setup_bridge(hose, dev, sub_bus); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 354 | |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 355 | sub_bus = hose->current_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 356 | break; |
| 357 | |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 358 | case PCI_CLASS_BRIDGE_CARDBUS: |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 359 | /* |
| 360 | * just do a minimal setup of the bridge, |
| 361 | * let the OS take care of the rest |
| 362 | */ |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 363 | pciauto_setup_device(hose, dev, 0, pci_mem, |
| 364 | pci_prefetch, pci_io); |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 365 | |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 366 | debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n", |
| 367 | PCI_DEV(dev)); |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 368 | |
| 369 | hose->current_busno++; |
| 370 | break; |
| 371 | |
TsiChung Liew | f33fca2 | 2008-03-30 01:19:06 -0500 | [diff] [blame] | 372 | #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE) |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 373 | case PCI_CLASS_BRIDGE_OTHER: |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 374 | debug("PCI Autoconfig: Skipping bridge device %d\n", |
| 375 | PCI_DEV(dev)); |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 376 | break; |
| 377 | #endif |
Reinhard Arlt | c2e49f7 | 2009-07-25 06:19:12 +0200 | [diff] [blame] | 378 | #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349) |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 379 | case PCI_CLASS_BRIDGE_OTHER: |
| 380 | /* |
| 381 | * The host/PCI bridge 1 seems broken in 8349 - it presents |
| 382 | * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_ |
| 383 | * device claiming resources io/mem/irq.. we only allow for |
| 384 | * the PIMMR window to be allocated (BAR0 - 1MB size) |
| 385 | */ |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 386 | debug("PCI Autoconfig: Broken bridge found, only minimal config\n"); |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 387 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, |
| 388 | hose->pci_prefetch, hose->pci_io); |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 389 | break; |
| 390 | #endif |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 391 | |
| 392 | case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */ |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 393 | debug("PCI AutoConfig: Found PowerPC device\n"); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 394 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 395 | default: |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 396 | pciauto_setup_device(hose, dev, 6, pci_mem, |
| 397 | pci_prefetch, pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 398 | break; |
| 399 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 400 | |
| 401 | return sub_bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 402 | } |